Flutter shows a red error screen saying RenderBox was not laid out
RenderBox was not laid out:Short answer
RenderBox was not laid out is rarely the real error. It is usually a follow-up of an earlier one in the console, such as Vertical viewport was given unbounded height or an unbounded TextField width. Scroll up to the first error, then give that widget a bounded size with Expanded, Flexible or SizedBox.
Fix it fast
1Find out which cause you have
Do not debug the RenderBox line. Run the app and read the console from the top: the first assertion that was thrown during layout is the one to fix.
flutter run 2>&1 | head -n 602Match what you see, then jump to its fix
What does the error look like?
RenderBox was not laid out:RenderViewport#5a477 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATEVertical viewport was given unbounded height.An InputDecorator, which is typically created by a TextField, cannot have an unbounded width.
Cause 1 of 2
A ListView or GridView sits inside a Column
1Check
Look for a scrollable widget that is a direct child of a Column. A Column gives its children no height limit, and a scrollable tries to fill all the height it is offered.
grep -rn -B2 -A4 "Column(" lib/ | grep -n "ListView\|GridView"2Fix
Give the scrollable a bounded height. Wrap it in Expanded to fill the space left in the Column. Use SizedBox for a fixed height or Flexible for a share of it.
Column(
children: <Widget>[
const Text('Header'),
Expanded(
child: ListView(
children: const <Widget>[
ListTile(title: Text('Map')),
],
),
),
],
)3Verify
Hot reload. The red screen is gone and the list scrolls under the header.
flutter analyzeConfirmed by docs.flutter.dev
Cause 2 of 2
A TextField sits directly inside a Row
1Check
Find the TextField or TextFormField whose parent is a Row. A Row gives its children no width limit, and the text field has no width of its own.
grep -rn -B3 "TextField(\|TextFormField(" lib/ | grep "Row("2Fix
Constrain the field with Expanded, or with a SizedBox of a fixed width.
Row(
children: [
Expanded(child: TextFormField()),
],
)3Verify
Hot reload and type into the field. It now fills the free width of the row.
flutter runConfirmed by docs.flutter.dev
How do I stop it coming back?
- When a red screen appears, read the first error in the console, not the last one.
- Before putting a ListView, GridView or TextField inside a Row or Column, decide where its size limit comes from.
- Run the screen on a small phone size in the emulator to catch layout errors early.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix