Listview.builder inside the column what error it will throw and why ?
Answer
Overview
Placing a
text
ListView.buildertext
ColumnThe Error
textflutter: The following assertion was thrown during performLayout(): flutter: RenderShrinkWrappingViewport does not support returning intrinsic dimensions.
Or:
textflutter: Vertical viewport was given unbounded height.
Why Does This Happen?
- gives its children infinite height — it does not constrain vertical space.text
Column - also wants infinite height to render its scroll area.text
ListView.builder - When is insidetext
ListView, both widgets ask for infinite height — this conflict causes Flutter to throw an error.textColumn
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
dartColumn( 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)
dartColumn( 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:
forces Flutter to render ALL items at once — this defeats the purpose oftextshrinkWrap: trueand kills performance for large lists.textListView.builder
Best Practice Summary
| Approach | Performance | Use When |
|---|---|---|
text | ✅ Best | ListView should fill remaining space |
text | ✅ Good | You know exact height needed |
text | ❌ Avoid for large lists | Small static lists only |
Golden Rule: Always give
a bounded height constraint when inside atextListView. UsetextColumnin most cases.textExpanded