Sub topic 10
Build long lists lazily and keep item identity stable
A long list can make a large screen slow in two different ways:
- Building every item at once uses more memory and increases initial build time.
- Losing an item's identity can move state to the wrong row when items are inserted, removed, or reordered.
This chapter assumes that you know basic Dart syntax, StatelessWidget, StatefulWidget, setState, and the build method.
Build list items lazily
ListView.builder creates list children when they are needed for the viewport. It does not immediately build every item in a large collection. As the user scrolls, Flutter builds more children and can remove children that are far outside the visible area.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ProductPage()));
}
class ProductPage extends StatelessWidget {
const ProductPage({super.key});
@override
Widget build(BuildContext context) {
final products = List.generate(
1000,
(index) => 'Product ${index + 1}',
);
return Scaffold(
appBar: AppBar(title: const Text('Products')),
body: ListView.builder(
itemCount: products.length,
itemBuilder: (context, index) {
return ProductTile(name: products[index]);
},
),
);
}
}
class ProductTile extends StatelessWidget {
const ProductTile({required this.name, super.key});
final String name;
@override
Widget build(BuildContext context) {
return ListTile(
leading: const Icon(Icons.shopping_bag),
title: Text(name),
);
}
}Use ListView(children: ...) for short, already-known lists. Use ListView.builder when the list may be large or when creating an item is unnecessary until it is near the viewport.
Keep item identity stable with keys
A key is an identifier that helps Flutter match a widget with the corresponding existing element. This matters when the order or contents of a list change.
Without keys, Flutter generally matches children by their position. If the first row is removed, the old second row may be reused for the new first row. That reuse is usually efficient, but it can attach state to the wrong data.
A ValueKey uses a stable value, such as a database ID:
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return ProductTile(
key: ValueKey(item.id),
product: item,
);
},
)The ID must identify the item uniquely among its siblings. Do not use the current list index as the key when rows can be reordered or removed. The index describes a position, not an item.
| Situation | Recommended approach |
|---|---|
| Immutable short list | `ListView(children: ...)` |
| Large or scrolling list | `ListView.builder` |
| Stateful rows that can move | `ValueKey(item.id)` |
| Rows that never move and have no state | A key may not be needed |
Understand what keys do not solve
Keys do not make an expensive build method cheap. They do not prevent a visible row from rebuilding when its parent rebuilds. They preserve identity so Flutter can reuse the correct element and state.
Keep row widgets focused. Avoid decoding large files, sorting data, or making network requests inside itemBuilder. Prepare data outside the builder, and use a separate widget for complex rows.
Lazy lists and state
A lazy list may remove an off-screen child. If a row contains temporary state that should survive scrolling, consider whether the state belongs in a model or whether the row needs a keep-alive strategy. Do not add keep-alive behavior automatically. Keeping thousands of rows alive increases memory use.
The following flow shows the normal relationship between scrolling, lazy construction, and keys.
Audit a long list
When reviewing a list, ask:
- Can the list contain enough items that eager construction is costly?
- Does
itemBuilderdo only small, local work? - Can an item move, disappear, or be inserted before another item?
- If so, does the row have a stable
ValueKey? - Should off-screen row state survive, or can it be recreated?
Exercise
A list displays editable messages. Messages can be deleted, and each row contains a text field. What should you use as the key, and why should you avoid the index?
Show answer
Use ValueKey(message.id). The message ID stays with the same message when rows move, while an index changes position and can attach a text field's state to the wrong message.