TExcelTAD
Home › Flutter › Flutter ListView

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.

flutter · try it yourself
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.

flutter · try it yourself
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.builder whenever the number of items is large or unknown (for example data loaded from a server). It can even handle an infinite list if you omit itemCount.

ListView.separated

Need dividers between rows? ListView.separated takes a second builder, separatorBuilder, which is called between items — never after the last one.

flutter · try it yourself
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.

flutter · try it yourself
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 ListView inside a Column throws an "unbounded height" error because both want to take all the vertical space. Wrap the list in Expanded so it fills only the remaining space.

Test yourself

Why is ListView.builder preferred for long lists?

  1. It sorts items automatically
  2. It only builds the items that are visible
  3. It adds dividers
  4. It caches network images

How many times is separatorBuilder called for a list of 5 items?

  1. 3
  2. 4
  3. 5
  4. 6

You put a ListView directly inside a Column and get an unbounded height error. A common fix is to…

  1. Wrap the ListView in Expanded
  2. Use Row instead
  3. Set reverse: true
  4. Remove itemCount