listview 出现Vertical viewport was given unbounded height 解决方法

解决方法一:

添加以下两行:

ListView.builder(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
...

In this code snippet, have a ListView.builder with the following properties:

scrollDirection: Axis.vertical: This property specifies the scrolling direction of the list. In this case, it is set to Axis.vertical, which means the list will scroll vertically. You can also set it to Axis.horizontal if you want the list to scroll horizontally.

shrinkWrap: true:
使用自己需要的空间
By default, a ListView takes up as much space as it can along its main axis. However, when you set shrinkWrap to true, the ListView will try to wrap its content and only take up as much space as needed. This can be helpful when you want to include a ListView inside another scrollable view or when the size of the list is not predetermined

解决方法二:
307

This generally happens when you try to use a ListView/GridView inside a Column, there are many ways of solving it, I am listing few here.

Wrap ListView in Expanded
使用expanded

Column(
  children: <Widget>[
    Expanded( // wrap in Expanded
      child: ListView(...),
    ),
  ],
)

Wrap ListView in SizedBox and give a bounded height
指定高度

Column(
  children: <Widget>[
    SizedBox(
      height: 400, // fixed height
      child: ListView(...),
    ),
  ],
)

Use shrinkWrap: true in ListView.

Column(
  children: <Widget>[
    ListView(
      shrinkWrap: true, // use this
    ),
  ],
)

参考:

https://stackoverflow.com/questions/50252569/vertical-viewport-was-given-unbounded-height

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值