Flutter ListView
Lists are everywhere in mobile apps: messages, contacts, products, settings. Flutter's ListView is a scrollable column of widgets. It comes in several flavours, and choosing the right one matters for performance once your list grows.
ListView with a fixed list of children
The simplest constructor takes a children list, just like Column, but scrolls when the content is taller than the screen.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: SettingsScreen()));
class SettingsScreen extends StatelessWidget {
const SettingsScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: ListView(
children: const [
ListTile(leading: Icon(Icons.wifi), title: Text('Wi-Fi')),
ListTile(leading: Icon(Icons.bluetooth), title: Text('Bluetooth')),
ListTile(leading: Icon(Icons.battery_full), title: Text('Battery')),
ListTile(leading: Icon(Icons.volume_up), title: Text('Sound')),
],
),
);
}
}This is fine for a handful of items. But every child is built immediately, even ones far off-screen.
ListView.builder for long lists
ListView.builder builds items lazily — only the rows that are visible (plus a small buffer) are created. You give it an itemCount and an itemBuilder that receives the index.
class ContactList extends StatelessWidget {
const ContactList({super.key});
static const names = ['Alice', 'Bao', 'Chen', 'Dara', 'Elif', 'Femi', 'Gus'];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: names.length,
itemBuilder: (context, index) {
final name = names[index];
return ListTile(
leading: CircleAvatar(child: Text(name[0])),
title: Text(name),
subtitle: Text('Contact #${index + 1}'),
onTap: () => debugPrint('Tapped $name'),
);
},
);
}
}Tapped Chen
Tip: Use
ListView.builderwhenever the number of items is large or unknown (for example data loaded from a server). It can even handle an infinite list if you omititemCount.
ListView.separated
Need dividers between rows? ListView.separated takes a second builder, separatorBuilder, which is called between items — never after the last one.
ListView.separated(
itemCount: 4,
separatorBuilder: (context, index) => const Divider(height: 1),
itemBuilder: (context, index) => ListTile(
title: Text('Order ${1001 + index}'),
trailing: const Icon(Icons.chevron_right),
),
)Horizontal lists
Set scrollDirection: Axis.horizontal to scroll sideways — common for carousels of cards. A horizontal list inside a vertical layout needs a fixed height, so wrap it in a SizedBox.
SizedBox(
height: 100,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 6,
itemBuilder: (context, i) => Container(
width: 100,
margin: const EdgeInsets.all(8),
color: Colors.teal[100 * (i + 1)],
alignment: Alignment.center,
child: Text('Item $i'),
),
),
)Choosing a constructor
| Constructor | Builds items | Best for |
|---|---|---|
ListView(children: …) |
All at once | Short, fixed lists |
ListView.builder |
Lazily, on demand | Long or dynamic lists |
ListView.separated |
Lazily, with separators | Lists that need dividers |
Note: A
ListViewinside aColumnthrows an "unbounded height" error because both want to take all the vertical space. Wrap the list inExpandedso it fills only the remaining space.
Test yourself
Why is ListView.builder preferred for long lists?
- It sorts items automatically
- It only builds the items that are visible
- It adds dividers
- It caches network images
How many times is separatorBuilder called for a list of 5 items?
- 3
- 4
- 5
- 6
You put a ListView directly inside a Column and get an unbounded height error. A common fix is to…
- Wrap the ListView in
Expanded - Use
Rowinstead - Set
reverse: true - Remove
itemCount