美文网首页
Flutter Failed assertion: line 1

Flutter Failed assertion: line 1

作者: 一只哈哈 | 来源:发表于2021-03-08 16:38 被阅读0次

报错 :Failed assertion: line 1785 pos 12: 'hasSize'

原因:1. SingleChildScrollView 嵌套 ListView、GridView报错 ,SingleChildScrollView 和 ListView 都有滚动
属性physics 他们默认是都是可以滚动的
2. SingleChildScrollView 添加了Expanded或者Flexible

解决方法: 1. 增加shrinkWrap: true 和 physics: NeverScrollableScrollPhysics()即可。

SingleChildScrollView(
  child: Column(
    children: <Widget>[
      GridView.count(
        crossAxisCount: 2,
        children: <Widget>[],
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
      ),
      ListView.builder(
        itemCount: 10,
        itemBuilder: (context, index) {
          return Text("test");
        },
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
      )
    ],
  ),
)

2.固定父级组件的高度

SingleChildScrollView(
    child: ConstrainedBox(
        constraints: BoxConstraints(maxHeight: MediaQuery.of(context).size.height),
        child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
                Expanded(
                    child: Text('Hello World!'),
                ),
            ],
        ),
    )
)

相关文章

网友评论

      本文标题:Flutter Failed assertion: line 1

      本文链接:https://www.haomeiwen.com/subject/ppnpqltx.html