Sub topic 9
Fixing overflow and unbounded-constraint errors
This chapter assumes you know what a Row and a Column are, how a parent passes constraints down to its children, and how Expanded divides leftover space. You should also be able to read a Flutter console error. The chapter covers the two layout errors you will meet most often, explains why each happens, and shows how to fix them.
Two errors, one root cause
Flutter's layout rule is that constraints go down and sizes go up. A parent gives each child a minimum and maximum width and height. The child picks a size inside those limits and reports it back.
Both errors in this chapter come from a mismatch between that rule and what a widget wants to do.
- Overflow: a child needs more space than the constraints allow, and it cannot shrink.
- Unbounded constraints: a child wants to fill all available space, but the parent says the space has no upper limit.
The first error is a visual warning. The second is a hard failure that stops the widget from laying out at all.
RenderFlex overflow
A Row or Column is a flex widget. Its render object is called RenderFlex. Along its main axis it gives non-flexible children unbounded room, then sums their sizes. If that sum is larger than the space the flex widget itself received, the extra is the overflow.
In debug mode you see a yellow and black striped bar at the edge of the widget. The console prints a message like this:
A RenderFlex overflowed by 182 pixels on the right.The message names the direction (here, the right edge) and the amount. It also lists the widget that caused it and its constraints. In release mode the stripes do not appear, but the content is still clipped or misplaced, so you must fix the cause.
A Row that overflows
This Row puts a long piece of text next to an icon. The text has no limit on its width, so it asks for its natural width, which is wider than the screen.
import 'package:flutter/material.dart';
void main() => runApp(const OverflowDemo());
class OverflowDemo extends StatelessWidget {
const OverflowDemo({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
child: Row(
children: [
const Icon(Icons.info),
const SizedBox(width: 8),
const Text(
'A very long message that would otherwise overflow the Row',
),
],
),
),
),
);
}
}On a phone-width screen this prints the overflow error. The Row is correct. The problem is that Text was never told how wide it may be.
The fix: give the child a bounded width
Expanded wraps the text and tells it to take only the width left after the icon and the gap. Now the text has a maximum width and can wrap or truncate.
Row(
children: [
const Icon(Icons.info),
const SizedBox(width: 8),
Expanded(
child: Text(
'A very long message that would otherwise overflow the Row',
overflow: TextOverflow.ellipsis,
),
),
],
)With overflow: TextOverflow.ellipsis, the text stays on one line and ends with an ellipsis. Remove that argument and the text wraps onto more lines instead, because Text wraps by default once its width is bounded.
Choosing between Expanded and Flexible
Both fix overflow, but they behave differently when the content is short.
| Widget | Child must fill its share? | Use it when |
|---|---|---|
| `Expanded` | Yes | The child should take all the remaining space |
| `Flexible` | No (default `FlexFit.loose`) | The child may be smaller than its share but must not exceed it |
For a short label in a wide row, Flexible lets the text hug its content. Expanded would stretch the text widget across the whole remaining width. That matters if you align the text or draw a background behind it.
Other ways to resolve overflow
Not every overflow should be fixed with Expanded. Pick the fix that matches what you want the user to see.
- Let the content wrap. Replace a
RowwithWrapwhen the children are chips, tags, or buttons that can drop to a new line. - Let the content scroll. Put the
Columnin aSingleChildScrollViewwhen the total height can legitimately exceed the screen, for example in a form with the keyboard open. - Shrink the content. Use
FittedBoxto scale a single child down to fit. - Truncate. Use
maxLinesandoverflowonText.
Do not hide the problem by wrapping the whole screen in a ClipRect or by setting a large fixed width. That removes the stripes without fixing the layout.
Unbounded constraints
The second error appears when a widget that tries to grow without limit is placed inside a parent that offers no limit.
A Column passes its children unbounded height along its main axis. A Row does the same for width. If one child is itself a widget that expands to fill its main axis, such as a ListView, it cannot decide on a size. Flutter then throws an exception such as:
Vertical viewport was given unbounded height.You may also see RenderBox was not laid out or Horizontal viewport was given unbounded width. These are follow-on messages. Read the first error in the console, because it names the real cause.
A ListView inside a Column
This is the most common case.
Column(
children: [
const Text('Header'),
ListView(
children: const [
ListTile(title: Text('One')),
ListTile(title: Text('Two')),
ListTile(title: Text('Three')),
],
),
],
)A ListView scrolls along its main axis, so it wants all the height it can get. The Column offers unbounded height. The two requests cannot be satisfied, and Flutter throws.
The fix: bound the height
Wrap the ListView in Expanded. The Column first lays out the non-flexible Text, then gives the Expanded child exactly the remaining height.
Column(
children: [
const Text('Header'),
Expanded(
child: ListView(
children: const [
ListTile(title: Text('One')),
ListTile(title: Text('Two')),
ListTile(title: Text('Three')),
],
),
),
],
)The Column itself must also have a bounded height for this to work. Directly under a Scaffold body it does, because the body receives the screen size.
Other ways to bound a child
| Approach | Effect | Trade-off |
|---|---|---|
| `Expanded` or `Flexible` | Child gets the remaining space | Parent flex widget must itself be bounded |
| `SizedBox(height: 200, child: ...)` | Fixed size | Does not adapt to screen size |
| `ListView(shrinkWrap: true)` | List sizes itself to its content | Builds every item, so it is slow for long lists |
| `ConstrainedBox` | Sets a maximum size | You must choose the numbers |
Use shrinkWrap: true only for short lists. It disables the lazy building that makes ListView efficient. For a long list inside a larger scrolling page, a CustomScrollView with slivers is the correct tool.
Related errors with the same cause
Two other messages are variations of the same problem.
Expandedinside an unbounded parent. UsingExpandedin aColumnthat is itself inside aSingleChildScrollViewfails, because the scroll view gives unbounded height and there is no "remaining space" to divide. The message usually says "RenderFlex children have non-zero flex but incoming height constraints are unbounded." Remove theExpandedor give theColumna fixed height.- Nested flex widgets. A
Rowinside aRowgives the inner row unbounded width. If the inner row containsExpanded, you get the same error. Wrap the inner row inExpanded, or remove the inner flex.
Reading the constraint chain
When you meet a new error, work from the child up to the parent and ask three questions.
- What constraints did this widget receive? The error text usually prints them, for example
BoxConstraints(0.0<=w<=Infinity, ...). - What does this widget want? Text wants its natural width. A
ListViewwants the whole axis. AContainerwith no child wants the maximum. - Which parent should bound it?
The first constraint with Infinity in the message points at the parent that failed to bound the child.
Summary of the fixes
| Symptom | Cause | First fix to try |
|---|---|---|
| Overflow by N pixels on the right | Child wider than Row | `Expanded` or `Flexible` around the child |
| Overflow by N pixels on the bottom | Children taller than Column | `SingleChildScrollView` or smaller content |
| Vertical viewport given unbounded height | Scrollable in a Column | `Expanded` around the scrollable |
| Non-zero flex with unbounded constraints | `Expanded` in a scrolling parent | Remove `Expanded` or bound the parent |
Exercise
A screen has a Column with a Text header and, below it, a ListView of 500 items. The app throws "Vertical viewport was given unbounded height." Which fix is better: wrapping the ListView in Expanded or setting shrinkWrap: true? Explain why.
Show answer
Wrap the ListView in Expanded. The Column lays out the Text first and gives the ListView exactly the remaining height, so the list keeps its lazy building and scrolls normally. shrinkWrap: true makes the list size itself to all 500 items, which builds every item up front and is slow.
Exercise
A Row contains an Icon, a SizedBox(width: 8), and a long Text. The Text is wrapped in Expanded and the overflow error is gone. You now want the Text to be only as wide as its content when the message is short. What single change do you make?
Show answer
Replace Expanded with Flexible. Flexible defaults to FlexFit.loose, so the Text can be smaller than its share of the space but still cannot exceed it.
Exercise
A Column inside a SingleChildScrollView contains an Expanded child. Flutter reports that children have non-zero flex but incoming height constraints are unbounded. Why does this happen, and give two ways to fix it.
Show answer
SingleChildScrollView gives its child unbounded height, so there is no remaining space for Expanded to divide. Fix it by removing Expanded and letting the child use its natural height, or by giving the Column a fixed height with a SizedBox or ConstrainedBox (or by removing the scroll view if the content always fits).