这是我的代码:

@override
Widget build(BuildContext context) {
  return new Material(
    color: Colors.deepPurpleAccent,
    child: new Column(
     mainAxisAlignment: MainAxisAlignment.center,
        children:<Widget>[new GridView.count(crossAxisCount: _column,children: new List.generate(_row*_column, (index) {
          return new Center(
              child: new CellWidget()
          );
        }),)]
    )
  );
}

例外情况如下:

I/flutter ( 9925): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 9925): The following assertion was thrown during performResize():
I/flutter ( 9925): Vertical viewport was given unbounded height.
I/flutter ( 9925): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 9925): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 9925): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter ( 9925): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter ( 9925): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter ( 9925): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter ( 9925): the height of the viewport to the sum of the heights of its children.
I/flutter ( 9925): 
I/flutter ( 9925): When the exception was thrown, this was the stack:
I/flutter ( 9925): #0      RenderViewport.performResize.<anonymous closure> (package:flutter/src/rendering/viewport.dart:827:15)
I/flutter ( 9925): #1      RenderViewport.performResize (package:flutter/src/rendering/viewport.dart:880:6)
I/flutter ( 9925): #2      RenderObject.layout (package:flutter/src/rendering/object.dart:1555:9)

当前回答

我面临这个问题,因为我想在一个ListView中显示一个GridView,解决方案是通过添加以下内容,不仅在ListView中,而且在GridView中:

收缩包装:真 scrollDirection: Axis.vertical

其他回答

所以,每个人都上传了答案,但没有人愿意解释原因: 我将复制关于shrinkWrap的文档:

Whether the extent of the scroll view in the [scrollDirection] should be determined by the contents being viewed. If the scroll view does not shrink wrap, then the scroll view will expand to the maximum allowed size in the [scrollDirection]. If the scroll view has unbounded constraints in the [scrollDirection], then [shrinkWrap] must be true. Shrink wrapping the content of the scroll view is significantly more expensive than expanding to the maximum allowed size because the content can expand and contract during scrolling, which means the size of the scroll view needs to be recomputed whenever the scroll position changes. Defaults to false.

所以,从文档的词汇表来看,这里发生的是我们的ListView处于一个无限约束的情况(在我们滚动的方向上),因此ListView会抱怨:

... 垂直视口被赋予无限的垂直空间 它需要扩展。

通过简单地将shrinkWrap设置为true,将确保它包装由内容定义的大小。示例代码说明:

// ...
  ListView(
    // Says to the `ListView` that it must wrap its
    // height (if it's vertical) and width (if it's horizontal).
    shrinkWrap: true,
  ),
// ...

这就是你的代码,尽管如此,@Rémi建议是最好的,使用Align而不是Column。

我想发布另一个答案,因为许多答案(包括已接受的答案!)建议在ListView.builder中使用shrinkWrap: true。虽然这消除了错误,但在这种情况下,由于性能原因,这种解决方案并不理想。Flutter设计了无限大小的ListView,专门在必要时阻止这种行为!

收缩包装滚动视图的内容比展开到允许的最大大小要昂贵得多,因为内容可以在滚动期间展开和收缩,这意味着每当滚动位置发生变化时,都需要重新计算滚动视图的大小。

在某些情况下/设备,这个属性导致故障滚动和动画滞后在我的项目。即使延迟不明显,这个属性也会影响滚动性能。

OP显示他正在为ListView创建一个小(有限)数量的子节点。如果我们想要解决这些列表的错误,谷歌建议使用一个Flexible小部件,即在列表本身上使用Expanded(带有flex 1的Flexible)。

Expanded(
    child: ListView(
        ...
    ),
) 

这里还有其他布局问题,但垂直视口无界高度是因为ListView没有一个限制其大小的父类。

有很多答案,但每一个都有自己的局限性。每个人都在说使用shrinkWrap:没错。是的,你应该使用它,但这些代码的效果是,当你滚动列表时,它会自动移动到顶部项目,这意味着滚动回顶部。 所以完美的解决方案是使用物理收缩包装,只有列表才能顺利滚动。

      shrinkWrap: true,          //
      physics: ScrollPhysics(),  // both line is mandatory

如果你想看Listview的其他例子。生成器与列作为子和列表可以垂直滚动,然后你可以看到这里的例子-在垂直列表中排列多个文本。

不要使用Column进行单子对齐。使用Align代替。

new Align(
  alignment: Alignment.topCenter,
  child: new GridView(),
)

也会遇到这种错误,因为代码是这样的

return Container(
      child: Column(
        children: [
          ListView.separated(
              itemBuilder: (context, index) {
                return CircleManagerSettingItem(widget.membersInfo[index]);
              },
              separatorBuilder: (_, index) => DD(
                    height: 1,
                    paddingLeft: 10,
                    paddingRight: 10,
                  ),
              itemCount: (widget.membersInfo.length)),
        ],
      ),
    );

删除容器内的列,然后确定。