Question #296MediumWidgets & UI

Listview.builder inside the column what error it will throw and why ?

Answer

Overview

Placing a

text
ListView.builder
directly inside a
text
Column
throws a RenderFlex overflow or more commonly a "Vertical viewport was given unbounded height" exception.


The Error

text
flutter: The following assertion was thrown during performLayout():
flutter: RenderShrinkWrappingViewport does not support returning intrinsic dimensions.

Or:

text
flutter: Vertical viewport was given unbounded height.

Why Does This Happen?

  • text
    Column
    gives its children infinite height — it does not constrain vertical space.
  • text
    ListView.builder
    also wants infinite height to render its scroll area.
  • When
    text
    ListView
    is inside
    text
    Column
    , both widgets ask for infinite height — this conflict causes Flutter to throw an error.
dart
// ❌ This throws an error
Column(
  children: [
    Text('Header'),
    ListView.builder( // ← ListView needs a height constraint!
      itemCount: 20,
      itemBuilder: (context, index) => ListTile(title: Text('Item $index')),
    ),
  ],
)

Fix 1: Wrap ListView in Expanded (Recommended)

dart
// ✅ Expanded gives ListView the remaining Column space
Column(
  children: [
    Text('Header'),
    Expanded(
      child: ListView.builder(
        itemCount: 20,
        itemBuilder: (context, index) => ListTile(title: Text('Item $index')),
      ),
    ),
  ],
)

Fix 2: Give ListView a Fixed Height via SizedBox

dart
Column(
  children: [
    Text('Header'),
    SizedBox(
      height: 300, // Fixed height
      child: ListView.builder(
        itemCount: 20,
        itemBuilder: (context, index) => ListTile(title: Text('Item $index')),
      ),
    ),
  ],
)

Fix 3: Use shrinkWrap (Use with Caution)

dart
Column(
  children: [
    Text('Header'),
    ListView.builder(
      shrinkWrap: true,        // ← ListView sizes itself to its content
      physics: NeverScrollableScrollPhysics(), // ← Disable inner scroll
      itemCount: 20,
      itemBuilder: (context, index) => ListTile(title: Text('Item $index')),
    ),
  ],
)

⚠️ Warning:

text
shrinkWrap: true
forces Flutter to render ALL items at once — this defeats the purpose of
text
ListView.builder
and kills performance for large lists.


Best Practice Summary

ApproachPerformanceUse When
text
Expanded
wrapping ListView
✅ BestListView should fill remaining space
text
SizedBox
with fixed height
✅ GoodYou know exact height needed
text
shrinkWrap: true
❌ Avoid for large listsSmall static lists only

Golden Rule: Always give

text
ListView
a bounded height constraint when inside a
text
Column
. Use
text
Expanded
in most cases.