当前位置: 首页 > 知识库问答 >
问题:

在Flatter中,当我在脚手架中添加ListView时,问题就出现了

薄鸿远
2023-03-14

我是一个新手,尝试在我的一条路线中添加一个列表视图,单击该列表视图时,会将我带到每个列表卡特定的新路线。

这是我的路线代码:

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      appBar: AppBar(
        title: Text('What you are in the mood for?'),
        backgroundColor: Colors.lightGreen[400],
        centerTitle: true,
      ),
      body: Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
        Text('Select a genre:',
            textAlign: TextAlign.center,
            style: TextStyle(
                fontSize: 40,
                color: Colors.black,
                fontWeight: FontWeight.bold)),


        //if I add ListView(...) here, I get errors


      ]),
    );
  }
}

我收到的问题是:

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
Tried to paint a RenderObject reentrantly.
The relevant error-causing widget was: 
  Scaffold file:///Users/User/AndroidStudioProjects/choose_a_game_for_me/lib/main.dart:102:12
════════════════════════════════════════════════════════════════════════════════════════════════════

════════ Exception caught by rendering library ═════════════════════════════════════════════════════
The following assertion was thrown during paint():
Tried to paint a RenderObject reentrantly.

The following RenderObject was already being painted when it was painted again: RenderFlex#aafa2 relayoutBoundary=up5
...  needs compositing
...  parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body (can use size)
...  constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=603.4)
...  size: MISSING
...  direction: vertical
...  mainAxisAlignment: spaceEvenly
...  mainAxisSize: max
...  crossAxisAlignment: center
...  verticalDirection: down
Since this typically indicates an infinite recursion, it is disallowed.
The relevant error-causing widget was: 
  Scaffold file:///Users/Users/AndroidStudioProjects/choose_a_game_for_me/lib/main.dart:102:12
When the exception was thrown, this was the stack: 
#0      RenderObject._paintWithContext.<anonymous closure> (package:flutter/src/rendering/object.dart:2208:9)
#1      RenderObject._paintWithContext (package:flutter/src/rendering/object.dart:2221:6)
#2      PaintingContext.paintChild (package:flutter/src/rendering/object.dart:184:13)
#3      RenderBoxContainerDefaultsMixin.defaultPaint (package:flutter/src/rendering/box.dart:2515:15)
#4      RenderCustomMultiChildLayoutBox.paint (package:flutter/src/rendering/custom_layout.dart:404:5)
...
The following RenderObject was being processed when the exception was fired: RenderCustomMultiChildLayoutBox#8a3f0 relayoutBoundary=up4
...  needs compositing
...  parentData: <none> (can use size)
...  constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=683.4)
...  size: Size(411.4, 683.4)
RenderObject: RenderCustomMultiChildLayoutBox#8a3f0 relayoutBoundary=up4
  needs compositing
  parentData: <none> (can use size)
  constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=683.4)
  size: Size(411.4, 683.4)
...  child 1: RenderFlex#aafa2 relayoutBoundary=up5
...    needs compositing
...    parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body (can use size)
...    constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=603.4)
...    size: MISSING
...    direction: vertical
...    mainAxisAlignment: spaceEvenly
...    mainAxisSize: max
...    crossAxisAlignment: center
...    verticalDirection: down
...    child 1: RenderParagraph#e94b7 relayoutBoundary=up6 NEEDS-PAINT
...      parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
...      constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=Infinity)
...      size: Size(343.0, 47.0)
...      textAlign: center
...      textDirection: ltr
...      softWrap: wrapping at box width
...      overflow: clip
...      locale: en_US
...      maxLines: unlimited
...      text: TextSpan
...        debugLabel: ((englishLike body1 2014).merge(blackMountainView bodyText2)).merge(unknown)
...        inherit: false
...        color: Color(0xff000000)
...        family: Roboto
...        size: 40.0
...        weight: 700
...        baseline: alphabetic
...        decoration: TextDecoration.none
...        "Select a genre:"
...    child 2: RenderRepaintBoundary#02cc2 relayoutBoundary=up6 NEEDS-PAINT
...      needs compositing
...      parentData: offset=Offset(0.0, 0.0); flex=null; fit=null (can use size)
...      constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=Infinity)
...      size: MISSING
...      usefulness ratio: no metrics collected yet (never painted)
...      child: RenderCustomPaint#cd675 relayoutBoundary=up7 NEEDS-PAINT
...        needs compositing
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=Infinity)
...        size: MISSING
...        child: RenderRepaintBoundary#1d1e0 relayoutBoundary=up8 NEEDS-PAINT
...          needs compositing
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(0.0<=w<=411.4, 0.0<=h<=Infinity)
...          size: MISSING
...          usefulness ratio: no metrics collected yet (never painted)
...  child 2: RenderConstrainedBox#4a2e7 relayoutBoundary=up5 NEEDS-PAINT
...    needs compositing
...    parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.appBar (can use size)
...    constraints: BoxConstraints(w=411.4, 0.0<=h<=683.4)
...    size: Size(411.4, 80.0)
...    additionalConstraints: BoxConstraints(0.0<=w<=Infinity, 0.0<=h<=80.0)
...    child: RenderSemanticsAnnotations#997db relayoutBoundary=up6 NEEDS-PAINT
...      needs compositing
...      parentData: <none> (can use size)
...      constraints: BoxConstraints(w=411.4, 0.0<=h<=80.0)
...      semantic boundary
...      size: Size(411.4, 80.0)
...      child: RenderAnnotatedRegion<SystemUiOverlayStyle>#e6930 relayoutBoundary=up7 NEEDS-PAINT
...        needs compositing
...        parentData: <none> (can use size)
...        constraints: BoxConstraints(w=411.4, 0.0<=h<=80.0)
...        size: Size(411.4, 80.0)
...        child: RenderPhysicalModel#35a1a relayoutBoundary=up8 NEEDS-PAINT
...          needs compositing
...          parentData: <none> (can use size)
...          constraints: BoxConstraints(w=411.4, 0.0<=h<=80.0)
...          size: Size(411.4, 80.0)
...          elevation: 4.0
...          color: Color(0xff9ccc65)
...          shadowColor: Color(0xff9ccc65)
...          shape: BoxShape.rectangle
...          borderRadius: BorderRadius.zero
...  child 3: RenderStack#16a71 NEEDS-LAYOUT NEEDS-PAINT
...    parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.floatingActionButton
...    constraints: MISSING
...    size: MISSING
...    alignment: centerRight
...    textDirection: ltr
...    fit: loose
...    overflow: clip
...    child 1: RenderTransform#68acc NEEDS-LAYOUT NEEDS-PAINT
...      parentData: not positioned; offset=Offset(0.0, 0.0)
...      constraints: MISSING
...      size: MISSING
...      transform matrix: [0] 0.0,0.0,0.0,0.0
[1] 0.0,0.0,0.0,0.0
[2] 0.0,0.0,1.0,0.0
[3] 0.0,0.0,0.0,1.0
...      origin: null
...      alignment: center
...      textDirection: ltr
...      transformHitTests: true
...      child: RenderTransform#9de5f NEEDS-LAYOUT NEEDS-PAINT
...        parentData: <none>
...        constraints: MISSING
...        size: MISSING
...        transform matrix: [0] 0.7,0.7,0.0,0.0
[1] -0.7,0.7,0.0,0.0
[2] 0.0,0.0,1.0,0.0
[3] 0.0,0.0,0.0,1.0
...        origin: null
...        alignment: center
...        textDirection: ltr
...        transformHitTests: true
════════════════════════════════════════════════════════════════════════════════════════════════════

我尝试让ListView成为自己的类,然后将其添加到我的SecondRoute主体中,但同样的错误出现了。

102号线是二年级的脚手架。

谢谢你的帮助

共有1个答案

何玺
2023-03-14

如果试图在列中添加列表视图,则必须将其包装在展开的列表中,如下所示:

Column(
  children: [
   ...
    Expanded(
      child: ListView(...),
    ),
  ]
)

这是因为ListView将尝试占用所有可用空间,但这将导致RenderFlex溢出。扩展的小部件本质上告诉ListView获取所有可用空间,并向下滚动到列中无法容纳的其余空间。

 类似资料:
  • 我正在尝试实现一个水平列表视图,其中显示了从Flatter image_picker插件中提取的图像。 它的工作正常,如果我不使用列表视图,只显示一个单一的图像。然而,我试图使用多个图像,只要我在列表视图中的小部件只是显示为黑色。我的列表视图代码如下: 我没有显示整个代码,因为页面太长,但是页面的其余部分工作正常。如果我删除Listview构建器,而只是使用下面的测试,它就可以正常工作。我做错了什

  • 从通过API提交的表单中发布数据是成功的。 但是,在将X-CSRF-TOKEN添加到头并设置之后,结果数据没有发布到名为 错误: 加载失败http://localhost/simple_api/insert.php:对飞行前请求的响应未通过访问控制检查:当请求的凭据模式为“包括”时,响应中“访问控制允许来源”标头的值不得为通配符“*”。起源'http://localhost:4200因此,不允许访

  • 我是第一次开发颤振应用程序。。我在添加图像时遇到问题。我有以下问题: 在哪里创建图像文件夹? 在哪里添加资产标签pubspec.ymal? 这需要任何资产文件夹吗? 我所尝试的: 在pubspec内部。ymal: 完整文件: 错误日志: 我的主菜。dart代码: 我指的是这个教程https://flutter.io/tutorials/layout/ 此外,我想问,有没有在颤振清洁重建的工具,因为

  • 下面是我的global.java 加载时,我收到以下错误 ConstructorException:null;不能为标记构造java对象:yaml.org,2002:models.post;Exception=无法为javabean=model.post@1创建property=author;找不到适合类模型的具有3个参数的构造函数。用户 知道为什么会这样吗?

  • 当我想更新JavaFX中的时,出现了一个问题。 调试很混乱,因为我找不到异常的原因。我在网上搜索了一下,但没有找到对我有用的东西。希望有人能帮我解决这个问题。

  • 我在laravel 5.3中工作,我想在现有表格中添加列(即课程章节)。所以我已经通过命令创建了迁移文件 迁移文件已经创建,我已经添加了一些代码来在该表中添加列,如下所示 我执行命令后 PHP工匠迁移 表没有被创建,而不是我得到了错误 任何答案? 我现有的表迁移文件是