Sub topic 5
ListView.builder for Long and Dynamic Lists
This chapter assumes basic Dart lists and closures, Flutter widget basics, StatefulWidget, and familiarity with Column and Expanded. You should also know how to run an app and use hot reload.
Why use `ListView.builder`?
ListView.builder creates list items on demand. Flutter builds the items that are visible, plus a small area near the viewport, instead of creating every item immediately.
This lazy construction keeps memory use and initial build time lower for long or dynamic lists. The list can contain hundreds or thousands of records without requiring one widget for every record at startup.
Use the regular ListView constructor when the list is short and its children are known:
ListView(
children: const [
ListTile(title: Text('Profile')),
ListTile(title: Text('Settings')),
ListTile(title: Text('About')),
],
)Use ListView.builder when items come from data, the number of items can change, or the list is large.
Build a data-driven list
The itemCount property tells Flutter how many items exist. The itemBuilder callback receives a BuildContext and an integer index. It must return the widget for that position.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: MessagesPage()));
}
class MessagesPage extends StatelessWidget {
const MessagesPage({super.key});
final List<String> messages = const [
'Welcome to Flutter',
'Your report is ready',
'Meeting starts at 10:00',
'Backup completed',
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Messages')),
body: ListView.builder(
itemCount: messages.length,
itemBuilder: (context, index) {
return ListTile(
leading: const Icon(Icons.mail),
title: Text(messages[index]),
subtitle: Text('Message ${index + 1}'),
);
},
),
);
}
}Flutter calls itemBuilder with indexes from 0 through itemCount - 1. Keep the callback fast. Read the item at index, construct its widget, and avoid expensive work inside the callback.
If the data changes, rebuild the list with the new length. A stateful example might append an item like this:
setState(() {
messages.add('A new message');
});The new messages.length then becomes the list's new itemCount.
Constraints inside a Column
A vertical ListView needs a finite height. A Column normally gives flexible children as much height as they request, so placing a list directly inside it often causes an unbounded-height error.
Give the list the remaining height with Expanded:
Column(
children: [
const Text('Inbox'),
Expanded(
child: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(title: Text('Row $index'));
},
),
),
],
)shrinkWrap: true makes a list measure all its children and take only the space it needs. It can help for a short nested list, but it reduces the benefit of lazy scrolling and may be slow for large lists. Prefer one main scrollable or use Expanded when the list should fill available space.
How lazy building works
Widgets that leave the useful viewport area can be removed from the active layout. Flutter can later build them again when the user scrolls back.
Exercise
You have 10,000 records and a vertical list inside a Column. Which two changes are usually appropriate?
Show answer
Use ListView.builder with itemCount set to the record count, and wrap it in Expanded so the list has a bounded height.