DEV3 Technologies home

DEV3 Developer Forum

How do I fix "Vertical viewport was given unbounded height" when a ListView is inside a Column?

Flutter helpSolved0 views
dev3 ·

I put a ListView.builder inside a Column under a header and the screen goes red:

Vertical viewport was given unbounded height.
Viewports expand in the scrolling direction to fill their container.

Where should the list go?

#flutter#listview#layout

0

1 reply

dev3 · Accepted answer

Why it happens

A Column gives its children unbounded height. A ListView wants to expand to fill whatever height it is given, so with infinite height it has nothing to size itself against and throws.

Fix 1 (almost always right): Expanded

Column(
  children: [
    const Header(),
    Expanded(
      child: ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, i) => ItemTile(items[i]),
      ),
    ),
  ],
)

The list now gets exactly the remaining height and stays lazy (only visible items are built).

Fix 2: shrinkWrap for short lists

ListView(
  shrinkWrap: true,
  physics: const NeverScrollableScrollPhysics(),
  children: [...],
)

shrinkWrap: true measures every child, so it is O(n) and defeats lazy building. Use it only for a handful of items (for example a list of 5 rows inside a bigger scrolling page).

Fix 3: one scroll view with slivers

If the header and the list should scroll together, use a CustomScrollView with SliverToBoxAdapter for the header and SliverList.builder (or SliverList(delegate: SliverChildBuilderDelegate(...)) on older versions) for the items. This is the cleanest answer for mixed content and keeps lazy building.

Pitfalls

  • Nested scrollables in the same direction fight over gestures; give only one of them scrolling physics.
  • The same error appears for GridView and for a horizontal ListView inside a Row; the fix is identical (Expanded, or a fixed SizedBox(height: ...) for a horizontal strip).
0

Your reply

Every post is reviewed by DEV3 before it appears. Usually reviewed within 24 hours.

Markdown supported. Use ``` for code blocks. Images are not allowed.