flutter
runtime_error
ai_generated
true
FlutterError:RenderBox 未布局:RenderRepaintBoundary#12345 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
FlutterError: RenderBox was not laid out: RenderRepaintBoundary#12345 relayoutBoundary=up1 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
ID: flutter/render-box-intersect-error
85%修复率
88%置信度
1证据数
2023-03-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Flutter 3.7 | active | — | — | — |
| Flutter 3.10 | active | — | — | — |
| Dart 3.0 | active | — | — | — |
根因分析
在 RenderBox 布局完成前访问其几何属性,通常是因为在 initState 或第一帧之前调用了 RenderBox.size。
English
A widget's RenderBox is accessed for geometry before it has been laid out, often due to calling RenderBox.size in initState or before the first frame.
官方文档
https://api.flutter.dev/flutter/rendering/RenderBox/size.html解决方案
-
Delay RenderBox access until after the first frame using WidgetsBinding.instance.addPostFrameCallback and then access the GlobalKey's currentContext.size.
-
Use a LayoutBuilder to get constraints and size without directly accessing RenderBox.
无效尝试
常见但无效的做法:
-
70% 失败
SizedBox forces a size but does not ensure layout is complete before accessing RenderBox; the underlying widget may still not be laid out.
-
50% 失败
addPostFrameCallback runs after the first frame, but if accessed before the callback fires, the error persists.
-
60% 失败
The context may exist but the RenderBox may not be laid out yet; size is null until layout completes.