android - Flutter 嵌套 ListView.builder 抛出错误

标签 android ios mobile flutter

我想将两个 ListView.builder 放在一个小部件屏幕中,但出现以下错误:

The following assertion was thrown during performLayout():
I/flutter (17103): RenderFlex children have non-zero flex but incoming         
height constraints are unbounded.
I/flutter (17103): When a column is in a parent that does not provide a     
finite height constraint, for example if it is
I/flutter (17103): in a vertical scrollable, it will try to shrink-wrap its 
children along the vertical axis. Setting a
I/flutter (17103): flex on a child (e.g. using Expanded) indicates that the 
child is to expand to fill the remaining
I/flutter (17103): space in the vertical direction.
I/flutter (17103): These two directives are mutually exclusive. If a parent 
is to shrink-wrap its child, the child
I/flutter (17103): cannot simultaneously expand to fit its parent.
I/flutter (17103): Consider setting mainAxisSize to MainAxisSize.min and 
using FlexFit.loose fits for the flexible
I/flutter (17103): children (using Flexible rather than Expanded). This will 
allow the flexible children to size
I/flutter (17103): themselves to less than the infinite remaining space they 
would otherwise be forced to take, and
I/flutter (17103): then will cause the RenderFlex to shrink-wrap the 
children rather than expanding to fit the maximum
I/flutter (17103): constraints provided by the parent.
I/flutter (17103): The affected RenderFlex is:
I/flutter (17103):   RenderFlex#446cb relayoutBoundary=up14 NEEDS-LAYOUT 
NEEDS-PAINT

这是我的代码:

 @override
 Widget build(BuildContext context) {
if (!loaded) _loadZones();
return new Scaffold(
    body: new Column(
  children: <Widget>[
    new Padding(padding: EdgeInsets.fromLTRB(0, 20, 0, 0)),
    new Expanded(
        child: new ListView.builder(
            itemCount: zones.length,
            itemBuilder: (BuildContext context, int index) {
              Zone zone = zones[index];
              List<Place> places = zone.places;
              print(zone.toString());
              print(places.length);
              return InkWell(
                  onTap: () {
                    Navigator.push(
                      context,
                      FromRightToLeft(
                          builder: (context) => CondominiumScreen(zone.id)),
                    );
                  },
                  child: Card(
                      color: Colors.white,
                      key: Key(zone.name),
                      margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
                      child: new InkWell(
                          child: new Column(
                        children: <Widget>[
                          ListTile(
                            leading: Icon(Icons.place),
                              title: Row(
                            children: <Widget>[
                              Icon(
                                Icons.place,
                                color: Colors.grey,
                              ),
                              Text(
                                ' ${zone.name}',
                                style: TextStyle(fontSize: 20),
                              ),
                            ],
                          )),
                          new Divider(),
                          new Flexible(
                            child: new ListView.builder(
                                itemCount: places.length,
                                itemBuilder: (BuildContext ct, int i) {
                                  Place place = places[i];
                                  return Text(
                                    "● ${place.name}",
                                    textAlign: TextAlign.start,
                                  );
                                }),
                          ),
                          new Divider(),
                          new Row(
                            children: <Widget>[
                              Padding(
                                padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                              ),
                              Expanded(
                                flex: 50,
                                child: InkWell(
                                    child: FlatButton(
                                        child: const Text('Reportes'),
                                        shape: RoundedRectangleBorder(
                                            side: BorderSide(
                                                color: Colors.black12)),

block 引用

              splashColor: Colors.white10,
                                        color: Colors.white,
                                        onPressed: () {
                                          /* ... */
                                        })),
                              ),
                              Padding(
                                  padding: EdgeInsets.fromLTRB(5, 0, 5, 0)),
                              Expanded(
                                flex: 50,
                                child: InkWell(
                                    child: FlatButton(
                                        child: const Text('Turnos'),
                                        shape: RoundedRectangleBorder(
                                            side: BorderSide(
                                                color: Colors.black12)),
                                        splashColor: Colors.white10,
                                        color: Colors.white,
                                        onPressed: () {
                                          /* ... */
                                        })),
                              ),
                              Padding(
                                padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                              ),
                            ],
                          ),
                        ],
                      ))));
            }))
  ],
));

我已经尝试在每一列中将 mainAxisSize 设置为 MainAxisSize.min,但它不起作用。

在 performLayout() 期间抛出了以下断言:

I/flutter (17956): RenderFlex children have non-zero flex but incoming height constraints are unbounded.
I/flutter (17956): When a column is in a parent that does not provide a finite height constraint, for example if it is
I/flutter (17956): in a vertical scrollable, it will try to shrink-wrap its children along the vertical axis. Setting a
I/flutter (17956): flex on a child (e.g. using Expanded) indicates that the child is to expand to fill the remaining
I/flutter (17956): space in the vertical direction.
I/flutter (17956): These two directives are mutually exclusive. If a parent is to shrink-wrap its child, the child
I/flutter (17956): cannot simultaneously expand to fit its parent.
I/flutter (17956): Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits for the flexible
I/flutter (17956): children (using Flexible rather than Expanded). This will allow the flexible children to size
I/flutter (17956): themselves to less than the infinite remaining space they would otherwise be forced to take, and
I/flutter (17956): then will cause the RenderFlex to shrink-wrap the children rather than expanding to fit the maximum
I/flutter (17956): constraints provided by the parent.

最佳答案

为了修复代码中的给定错误 - 您需要做两件事 - 在 ListView.builder 中添加 shr​​inkWrap: true > & 在第二个 ListView.builder 中删除了 Flexible -

更新后的代码如下:

   Widget build(BuildContext context) {
    if (!loaded) _loadZones();
    return new Scaffold(
        body: new Column(
      children: <Widget>[
        new Padding(padding: EdgeInsets.fromLTRB(0, 20, 0, 0)),
        new Expanded(
            child: new ListView.builder(
                shrinkWrap: true,   //Added
                itemCount: zones.length,
                itemBuilder: (BuildContext context, int index) {
                  Zone zone = zones[index];
                  List<Place> places = zone.places;
                  print(zone.toString());
                  print(places.length);
                  return InkWell(
                      onTap: () {
                        Navigator.push(
                          context,
                          FromRightToLeft(
                              builder: (context) => CondominiumScreen(zone.id)),
                        );
                      },
                      child: Card(
                          color: Colors.white,
                          key: Key(zone.name),
                          margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
                          child: new InkWell(
                              child: new Column(
                            children: <Widget>[
                              ListTile(
                                  leading: Icon(Icons.place),
                                  title: Row(
                                    children: <Widget>[
                                      Icon(
                                        Icons.place,
                                        color: Colors.grey,
                                      ),
                                      Text(
                                        ' ${zone.name}',
                                        style: TextStyle(fontSize: 20),
                                      ),
                                    ],
                                  )),
                              new Divider(),
                              new ListView.builder(   //removed Flexible
                                  shrinkWrap: true, //Added
                                  itemCount: places.length,
                                  itemBuilder: (BuildContext ct, int i) {
                                    Place place = places[i];
                                    return Text(
                                      "● ${place.name}",
                                      textAlign: TextAlign.start,
                                    );
                                  }),
                              new Divider(),
                              new Row(
                                children: <Widget>[
                                  Padding(
                                    padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                                  ),
                                  Expanded(
                                    flex: 50,
                                    child: InkWell(
                                        child: FlatButton(
                                            child: const Text('Reportes'),
                                            shape: RoundedRectangleBorder(
                                                side: BorderSide(
                                                    color: Colors.black12)),
                                            splashColor: Colors.white10,
                                            color: Colors.white,
                                            onPressed: () {
                                              /* ... */
                                            })),
                                  ),
                                  Padding(
                                      padding: EdgeInsets.fromLTRB(5, 0, 5, 0)),
                                  Expanded(
                                    flex: 50,
                                    child: InkWell(
                                        child: FlatButton(
                                            child: const Text('Turnos'),
                                            shape: RoundedRectangleBorder(
                                                side: BorderSide(
                                                    color: Colors.black12)),
                                            splashColor: Colors.white10,
                                            color: Colors.white,
                                            onPressed: () {
                                              /* ... */
                                            })),
                                  ),
                                  Padding(
                                    padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                                  ),
                                ],
                              ),
                            ],
                          ))));
                }))
      ],
    ));
  }

关于android - Flutter 嵌套 ListView.builder 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53861719/

相关文章:

html - 移动网络应用程序设计 - 仍然建议使用图像 Sprite 吗?

javascript - Hammer.js 平移事件仅适用于触摸设备,不适用于桌面计算机单击+拖动

java - 解析JSON数据出错,org.json.json异常,java.lang.String无法转换

java - 接口(interface)的所有方法都是抽象的吗?

javascript - 从 Android WebView 获取 iframe 数据

ios - 使用具有约束持续时间的动画快速移动按钮并在此期间检测触摸

android - 监听 Internet 变化(无网络检测)

ios - swift2.3 中的推送 View Controller 中导航栏颜色变为黑色

ios - telprompt VS 电话和应用批准

java - 如何使号码无法接通(类似于调用拦截器)?