The error, as Flutter prints it:
Vertical viewport was given unbounded height.
Viewports expand in the scrolling direction to fill their container. In this case, a vertical
viewport was given an unlimited amount of vertical space in which to expand. This situation
typically happens when a scrollable widget is nested inside another scrollable widget.
Usually the screen is blank or grey below the error, and you also see “RenderBox was not laid out” in the log.
Why it happens
A ListView tries to be as tall as the space it’s given, then scrolls inside that space. A Column and a SingleChildScrollView give their children unlimited height: “be as tall as you like”. So the ListView has no height to fill, and Flutter stops.
Which fix you need depends on what’s around the list.
Case 1: ListView inside a Column
Wrap the list in Expanded, so it gets the space left after the other children:
Column(
children: [
const TextField(decoration: InputDecoration(hintText: 'Search')),
Expanded(
child: ListView.builder(
itemCount: results.length,
itemBuilder: (context, i) => ListTile(title: Text(results[i])),
),
),
],
)
A fixed SizedBox(height: 300, child: ListView(...)) also works when the list really should be a set height.
Case 2: ListView inside SingleChildScrollView (or another ListView)
This is a scroll view inside a scroll view. The clean fix is one scroll view made of slivers:
CustomScrollView(
slivers: [
const SliverToBoxAdapter(child: ProfileHeader()),
SliverToBoxAdapter(
child: SizedBox(
height: 120,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: stories.length,
itemBuilder: (context, i) => StoryCard(stories[i]),
),
),
),
SliverList.builder(
itemCount: posts.length,
itemBuilder: (context, i) => PostTile(posts[i]),
),
],
)
Normal widgets go in SliverToBoxAdapter, and the long list becomes a SliverList that still builds items lazily. A horizontal list inside a vertical one needs a fixed height, as above.
Case 3: a short list inside a scroll view
For a handful of items, like tags or a few settings rows, shrinkWrap is fine:
ListView(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
children: [for (final t in tags) ListTile(title: Text(t))],
)
shrinkWrap: true makes the list exactly as tall as its children, and NeverScrollableScrollPhysics lets the outer scroll view do the scrolling. Don’t use it for long lists. It builds every item at once, which is slow with hundreds of rows. For only a few items, a plain Column is simpler still.
Related errors
- If the list is in a
Row, or is horizontal without a height, the message says “unbounded width” or “Horizontal viewport was given unbounded height”. The fix is the same idea:Expandedin a Row,SizedBox(height: …)for a horizontal list. - If content is too big rather than unbounded, you get “A RenderFlex overflowed by … pixels” instead.
Frequently asked questions
How do I put a ListView inside a Column in Flutter?
Wrap the ListView in Expanded (or Flexible) so it takes the remaining height of the Column. Give it a SizedBox with a fixed height instead if the list should be a specific size.
Is shrinkWrap: true bad?
It is fine for short lists. It makes the ListView measure and build every child to know its height, so for long or infinite lists it is slow. Use a CustomScrollView with SliverList for those.
How do I make a whole page with a header and a long list scroll together?
Use one CustomScrollView. Put the header in a SliverToBoxAdapter (or SliverAppBar) and the list in SliverList.builder. Everything scrolls as one, and list items are still built lazily.
Why does a horizontal ListView inside a vertical list need a height?
A horizontal list scrolls sideways, so it needs a fixed height to know how tall to be. Inside a vertical scroll view there is no height limit, so wrap it in a SizedBox with a height.
Stuck on a build or a store release? I fix and maintain Flutter apps, or tell me about your project. More fixes: all Flutter answers.