Flutter RenderObject 断言失败

Posted

技术标签:

【中文标题】Flutter RenderObject 断言失败【英文标题】:Flutter RenderObject assertion failure 【发布时间】:2021-07-24 13:15:01 【问题描述】:

我遇到以下错误:

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
Updated layout information required for RenderIndexedSemantics#f51aa NEEDS-LAYOUT to calculate semantics.
'package:flutter/src/rendering/object.dart':
Failed assertion: line 2658 pos 12: '!_needsLayout'


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

When the exception was thrown, this was the stack: 
#2      RenderObject._getSemanticsForParent (package:flutter/src/rendering/object.dart:2658:12)
#3      RenderObject._getSemanticsForParent.<anonymous closure> (package:flutter/src/rendering/object.dart:2680:61)
#4      Iterable.forEach (dart:core/iterable.dart:257:30)
#5      RenderSliverVariableSizeBoxAdaptor.visitChildren (package:flutter_staggered_grid_view/src/rendering/sliver_variable_size_box_adaptor.dart:296:29)
#6      RenderObject.visitChildrenForSemantics (package:flutter/src/rendering/object.dart:2765:5)
...
====================================================================================================

我想知道这是什么意思,通常什么时候发生? 我正在使用flutter_staggered_grid_view: ^0.4.0package。

如果需要,我将创建一个 GitHub 问题。

编辑: 这是一段代码来突出问题:

我的主要 Widget 由以下人员呈现:

StaggeredGridView.countBuilder(
      itemBuilder: (BuildContext context, int index) 
        return MyCustomItem();
      ,
      padding: EdgeInsets.only(left: Constants.maxWidth*2/100, right: Constants.maxWidth*2/100,
          bottom: Constants.maxHeight*10/100 + 65), // home button size + margin bottom
      crossAxisCount: 2,
      mainAxisSpacing: 20,
      crossAxisSpacing: Constants.maxWidth*2/100,
      itemCount: 30,
      physics: BouncingScrollPhysics(),
      staggeredTileBuilder: (int index) 
        return StaggeredTile.fit(1);
      ,
    ),

还有MyCustomItem 组成列表每个项目的小部件:

Column(
      children: [
        GestureDetector(
          child:
          Container(
            margin: (widget.index == 0 || widget.index == 1) ? EdgeInsets.only(top: Constants.maxHeight*6/100+60) : EdgeInsets.all(0),
            //width: postWidth,
            // TODO : post height
            child: ClipRRect(
              borderRadius: BorderRadius.all(Radius.circular(20)),
              child: Image.asset("assets/images/test.jpg"), // TODO 
            ),
          ),
          onTap: () 
            // TODO
          ,
          onDoubleTap: () 
            // TODO
          ,
        ),
        Row(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.min,
              children: [
                GestureDetector(child:
                Container(
                  margin: EdgeInsets.only(top: 10, bottom: 3),
                  width: 35,
                  height: 35,
                ),
                  onTap: () 
                    // TODO 
                  ,
                ),
                Flexible(child:
                Container(
                  width: Constants.maxWidth*25/100,
                  child:
                  Text(
                    "tessssst",
                    textAlign: TextAlign.center,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                      color: Color(resColors.getShadow()),
                    ),
                    maxLines: 1,
                  ),
                ),
                ),
              ],
            ),
            Spacer(),
            Container(
              margin: EdgeInsets.only(right: Constants.maxWidth * 2 / 100),
              child: Column(children: [
                Container(
                  margin: EdgeInsets.only(bottom: 3, top: 10),
                  child: InkWell(
                    onTap: () 
                      // TODO
                    ,
                    highlightColor: Colors.transparent,
                    splashColor: Colors.transparent,
                  ),
                ),
                Text("1029"),
              ]),
            ),
            Container(
              child: Column(children: [
                Container(
                  margin: EdgeInsets.only(bottom: 3, top: 10, right: Constants.maxWidth/100),
                  child: InkWell(
                    onTap: () => ,// TODO
                    highlightColor: Colors.transparent,
                    splashColor: Colors.transparent,
                  ),
                ),
                Text("Test"),
              ]),
            ),
          ],
        )
      ],
    );

我要补充一点,如果 MyCustomItem 只是一个 Text 小部件,我没有错误。

【问题讨论】:

请显示您的代码。 是的,你是对的,我应该把它放在首位。我已经完成了编辑 你试过没有GridView包吗? 它适用于ListView.builder,也适用于GridView.builder。不是StaggeredGridView。存在此错误,但似乎不会影响我的应用程序 UI,因为我有我想要的渲染并且一切正常......(使用 StaggeredGridView.builder 【参考方案1】:

您现在可以使用当前正在测试的 flutter_staggered_grid_view 存储库中的新 branch,但它已经为我解决了这个问题。

业主表示这是对插件的完整重构,修复了许多错误。

【讨论】:

以上是关于Flutter RenderObject 断言失败的主要内容,如果未能解决你的问题,请参考以下文章

Flutter核心类分析深入理解RenderObject

Flutter核心类分析深入理解RenderObject

flutter学习-Widget-Element-RenderObject

使用带有 Flutter 后退按钮的 onGenerateRoute 时断言失败

Flutter:ClipPath() 小部件中的断言失败错误

Flutter - 断言失败:第 61 行第 12 行:'_route == ModalRoute.of(context)':不正确