flutter中Cloumn的children中包含了一個ListView提示hasSize錯誤


代碼:
Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Shopping List'), ), body: Column( mainAxisSize: MainAxisSize.min, children: [ ListView( padding: EdgeInsets.symmetric(vertical: 8.0), children: widget.products .map((Product product) => ShoppingListItem( product: product, inCart: _shoppingCart.contains(product), onCartChanged: _handleCartChanged, )) .toList(), ), ], )); }
錯誤提示:

Vertical viewport was given unbounded height.
RenderBox was not laid out: RenderViewport#6b63c NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
'package:flutter/src/rendering/box.dart':
Failed assertion: line 1785 pos 12: 'hasSize'

Column中已經添加了MainAxisSize.min屬性,但還是報hasSize錯誤。

分析原因:根據ide的提示,垂直方向上給了一個無限大的高度,這個是因為子Widget是ListView,flutter中ListView在構建控件時會“詢問”子組件所要求的空間,而ListView無法給出具體值,導致出錯(粗略理解,后期分析源碼后再來分析)。
//TODO 后期有能力時分析源碼找出原因

解決辦法:將ListView放入一個能確定高度的Widget中。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM