Sub topic 9
Fixing Overflow and Unbounded-Constraint Errors
This chapter assumes basic Dart syntax, familiarity with StatelessWidget and build, and a working Flutter project. The examples focus on diagnosing layout errors rather than changing the visual design.
Understanding the two common errors
Flutter lays out a widget tree by passing constraints from parent to child. Each child chooses a size within those constraints, then reports its size back to the parent.
A RenderFlex overflow happens when a Row or Column cannot fit its children along its main axis. Flutter displays a yellow-and-black warning stripe at the edge. For example, a Row can overflow when a long Text sits beside an icon and there is not enough horizontal space.
An unbounded-constraint error happens when a widget that needs a finite limit receives unlimited space in one direction. A scrolling ListView is a common example. It must know its viewport height, but a Column normally gives children as much vertical space as they request.
Fixing a `Row` overflow
Give the text a share of the remaining width with Expanded. The text then receives a finite width and can truncate or wrap.
Row(
children: [
const Icon(Icons.info),
const SizedBox(width: 8),
Expanded(
child: Text(
'A very long message that would otherwise overflow the Row',
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
)Use Flexible when the child may use less space than its allocation. Use Expanded when the child must fill the available allocation. Use Spacer when you need flexible empty space between fixed children.
Other fixes depend on the intended design:
- Use
Wrapwhen items should move to another line. - Use a horizontal
SingleChildScrollViewwhen all content must remain on one line. - Reduce padding, font size, or fixed widths only when that matches the design.
- Avoid solving overflow by clipping important content.
A Column can overflow vertically when its total content is taller than the screen. Place the content in a scrollable widget, such as ListView, when the amount of content can vary.
Fixing unbounded height
When a scrollable widget is inside a Column, give it the remaining finite height with Expanded.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: ExamplePage()));
class ExamplePage extends StatelessWidget {
const ExamplePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Messages')),
body: Column(
children: [
const Padding(
padding: EdgeInsets.all(16),
child: Text('Inbox'),
),
Expanded(
child: ListView(
children: const [
ListTile(title: Text('One')),
ListTile(title: Text('Two')),
ListTile(title: Text('Three')),
],
),
),
],
),
);
}
}Do not put a normally scrolling ListView inside another vertical scroll view without a clear sizing plan. Nested scrolling can produce unbounded constraints and confusing behavior. If the list is short and should measure all its children, shrinkWrap: true may help, but it costs more layout work and is not a general performance fix.
A repeatable debugging process
When an error appears:
- Find the widget named in the exception, often
RenderFlexor a viewport. - Identify the direction of the problem: horizontal overflow usually involves a
Row; vertical overflow often involves aColumn. - Inspect each ancestor for missing size limits.
- Add
Expanded,Flexible,Wrap, or a scrollable widget according to the desired behavior. - Test with long text and small screen sizes.
Flutter DevTools’ Layout Explorer shows flex factors, available space, and child sizes. Use it to confirm which child consumes space and whether a parent supplied finite constraints.
Exercise
A ListView is a child of a Column and reports an unbounded-height error. What is the usual fix?
Show answer
Wrap the ListView in Expanded or Flexible so it receives a finite share of the remaining height.