flutter runtime_error ai_generated true

RenderBox 未布局:RenderViewport#abcde 需要具有 'size',但其约束为 'BoxConstraints(unconstrained)'

RenderBox was not laid out: RenderViewport#abcde needs to have a 'size' but its constraints are 'BoxConstraints(unconstrained)'

ID: flutter/rendering-box-height-nan

其他格式: JSON · Markdown 中文 · English
90%修复率
87%置信度
1证据数
2023-05-12首次发现

版本兼容性

版本状态引入弃用备注
3.0.0 active
3.10.0 active
3.16.0 active
3.22.0 active

根因分析

可滚动小部件(例如 ListView、GridView)放置在 Column 或 Row 中,未获得显式约束,导致高度无限。

English

A scrollable widget (e.g., ListView, GridView) is placed inside a Column or Row without being given explicit constraints, causing infinite height.

generic

官方文档

https://api.flutter.dev/flutter/rendering/RenderViewport-class.html

解决方案

  1. Wrap the scrollable widget in an Expanded or Flexible inside a Column or Row to give it bounded constraints.
  2. Use a CustomScrollView with slivers instead of nesting a ListView in a Column.
  3. Set a specific height constraint using SizedBox or Container with a height, but ensure it's enough for content.

无效尝试

常见但无效的做法:

  1. 40% 失败

    If content exceeds the fixed height, the ListView will overflow or clip; not adaptive.

  2. 35% 失败

    Expanded requires bounded constraints; if parent is unconstrained, it still fails.

  3. 25% 失败

    Does not give the ListView bounded constraints; the Column still tries to size itself to the ListView's infinite height.