Sub topic 15
Keeping Long Lists Fast in Flutter
This chapter assumes basic Dart syntax, Flutter widgets, StatelessWidget and StatefulWidget, Column, Expanded, and familiarity with running an app. It focuses on how Flutter builds, lays out, and updates long collections.
Build only what the viewport needs
A long list can contain thousands of records, but the screen shows only a small part of them. ListView.builder uses lazy building. Lazy building means Flutter creates item widgets as they are needed near the visible viewport instead of creating every item at once.
Use the builder constructor when the list is long, dynamic, or loaded from data:
import 'package:flutter/material.dart';
class ProductList extends StatelessWidget {
const ProductList({super.key, required this.products});
final List<String> products;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: products.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(products[index]),
);
},
);
}
}itemCount tells Flutter how many items exist. Supplying it lets Flutter calculate the scroll extent correctly and avoids requesting invalid indexes.
The default ListView constructor creates all widgets in its children list immediately. It is suitable for short, fixed lists, but it can use more memory for large collections.
Avoid expensive item work
Keep itemBuilder focused on creating the item. Do not perform network requests, database queries, sorting, or complex calculations there. Prepare data before building the list, or cache repeated results.
Prefer small item widgets. Mark widgets that never change as const where possible. For example, a constant icon can be reused efficiently:
ListTile(
leading: const Icon(Icons.check),
title: Text(tasks[index].name),
)Flutter can skip work when widget configuration remains unchanged. However, const does not fix slow code inside build. Profile the real bottleneck when performance matters.
Use stable keys when items have state
A key identifies a widget among its siblings. If a list can insert, remove, or reorder items, give stateful item widgets a stable key based on the item identity:
ListView.builder(
itemCount: tasks.length,
itemBuilder: (context, index) {
final task = tasks[index];
return TaskRow(
key: ValueKey(task.id),
task: task,
);
},
)Without a stable key, Flutter may reuse an item's state for a different row after the list changes. This can produce symptoms such as a checkbox appearing on the wrong task.
Understand the scrolling work
Flutter lays out the viewport first. The viewport requests enough children to fill the visible area and a small surrounding cache area. As the user scrolls, Flutter creates new children and removes distant ones from the active element tree.
Avoid shrinkWrap: true for a large list unless you need a list to size itself inside another scrollable. Shrink wrapping can require Flutter to measure more of the list and may repeat that work during updates. Prefer giving the list a bounded area with Expanded.
Measure before optimizing
Use Flutter DevTools to inspect frame timing, rebuilds, memory, and raster work. Test with realistic item counts and release-mode performance. A list that feels fast with ten items may expose layout or painting costs with ten thousand.
Exercise
Which change usually improves the initial performance of a 10,000-item list: replacing ListView.builder with ListView(children: ...), or using ListView.builder with itemCount?
Show answer
Use ListView.builder with itemCount. It lazily creates only the items needed near the viewport instead of creating all 10,000 item widgets immediately.