ListView.builder helps you show a long list of items on the screen efficiently. It creates only the items you see, saving memory and making your app faster.
ListView.builder in Flutter
ListView.builder( itemCount: numberOfItems, itemBuilder: (BuildContext context, int index) { return WidgetForItem(index); }, )
itemCount tells how many items to build.
itemBuilder is a function that creates each item widget using the current index.
ListView.builder( itemCount: 5, itemBuilder: (context, index) { return Text('Item $index'); }, )
ListView.builder( itemCount: 0, itemBuilder: (context, index) { return Text('No items'); }, )
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(items[index]),
);
},
)This app shows a scrollable list of fruits. Each fruit has a circle with its first letter and its name next to it.
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { final List<String> fruits = ['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry']; return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Fruit List')), body: ListView.builder( itemCount: fruits.length, itemBuilder: (context, index) { return ListTile( leading: CircleAvatar(child: Text(fruits[index][0])), title: Text(fruits[index]), ); }, ), ), ); } }
ListView.builder only builds visible items, so it uses less memory than ListView with all children.
Time complexity is O(n) for n visible items, not all items.
Common mistake: forgetting to set itemCount, which can cause infinite list or errors.
Use ListView.builder when you have many items or dynamic lists. Use ListView(children: [...]) for small fixed lists.
ListView.builder creates list items on demand for better performance.
Use itemCount to tell how many items to build.
itemBuilder builds each item widget using the index.
Practice
ListView.builder do in Flutter?Solution
Step 1: Understand ListView.builder purpose
ListView.builder creates list items lazily, meaning it builds only those items visible on screen.Step 2: Compare with other list types
Other list types like ListView create all items at once, which can be slow for large lists.Final Answer:
Creates list items only when they are visible on screen for better performance. -> Option AQuick Check:
ListView.builder = lazy item creation [OK]
- Thinking ListView.builder creates all items immediately
- Confusing ListView.builder with GridView
- Assuming ListView.builder creates buttons instead of list items
Solution
Step 1: Identify correct parameter names
The correct parameters areitemCountfor number of items anditemBuilderfor building each item.Step 2: Check syntax correctness
ListView.builder(itemCount: 10, itemBuilder: (context, index) => Text('Item $index')) uses correct parameter names and syntax withitemCount: 10anditemBuilderfunction.Final Answer:
ListView.builder(itemCount: 10, itemBuilder: (context, index) => Text('Item $index')) -> Option DQuick Check:
Correct parameters = ListView.builder(itemCount: 10, itemBuilder: (context, index) => Text('Item $index')) [OK]
- Using wrong parameter names like count or length
- Using buildItem instead of itemBuilder
- Missing itemCount parameter
ListView.builder(
itemCount: 3,
itemBuilder: (context, index) {
return Text('Number: $index');
},
)Solution
Step 1: Understand itemCount and index values
itemCount is 3, so index goes from 0 to 2.Step 2: Check itemBuilder output
Each item shows text 'Number: ' followed by the index value, so items are 'Number: 0', 'Number: 1', 'Number: 2'.Final Answer:
A list showing: Number: 0, Number: 1, Number: 2 -> Option BQuick Check:
Index starts at 0, so output matches A list showing: Number: 0, Number: 1, Number: 2 [OK]
- Assuming index starts at 1
- Expecting 3 items with index 1 to 3
- Thinking it causes an error due to index
ListView.builder(
itemCount: 5,
itemBuilder: (context, index) {
return Text('Item');
},
itemCount: 10,
)Solution
Step 1: Check parameters in ListView.builder
There are twoitemCountparameters, which is not allowed and causes a syntax error.Step 2: Verify other parts
The itemBuilder returns a Text widget correctly, and parameters are correct.Final Answer:
Duplicate itemCount parameter causes a syntax error. -> Option AQuick Check:
Duplicate keys in widget constructor cause errors [OK]
- Ignoring duplicate parameters
- Thinking missing return causes error here
- Assuming itemBuilder parameters are wrong
users = ['Anna', 'Bob', 'Cara'] using ListView.builder. Which code correctly shows each name with its position number starting from 1?Solution
Step 1: Adjust index to start numbering from 1
Since index starts at 0, add 1 to show position starting from 1.Step 2: Access user names correctly
Use users[index] to get the correct user name for each index.Final Answer:
ListView.builder(itemCount: users.length, itemBuilder: (context, index) => Text('${index + 1}. ${users[index]}')) -> Option CQuick Check:
Index + 1 for numbering, users[index] for names [OK]
- Using index without +1 for numbering
- Accessing users[index + 1] causing out-of-range error
- Using wrong variable name like i without declaration
Ready to prove it?
Pass the test and this pattern moves unknown
