Flutter – Build ListView from List of Items

Create Dynamic ListView using ListView.builder()

In this tutorial, we will learn how to build a ListView dynamically from one or more lists of items.

When you are developing a Flutter Application, many a times it is required that you want to show a list of items in a ListView. And you would not know the number of elements in the list beforehand. In such scenarios, you can use ListView.builder() constructor of ListView class to create a dynamic list of widgets in ListView widget.

Sample Code Snippet

Following is a sample code snippet to show list of items as a list of widgets in a ListView dynamically.

List<E> items = <E>[...];

ListView.builder(
  itemCount: items.length,
  itemBuilder: (BuildContext context, int index) {
    // access element from list using index
	// you can create and return a widget of your choice
    return <Widget>;
  }
)

Here, we are transforming items into ListView. itemCount property has to be assigned with the number of items in the list and itemBuilder builds the widget for each item in the list. Index and context are provided to the function of itemBuilder so that we can access item at index. You should return the widget from itemBuilder, and this widget is added to the ListView.

Example

This example Flutter Application demonstrate how to build a dynamic ListView from a list of items using ListView.builder().

We are taking two lists: the first one contains names of persons and the second one contains number of messages from each of the person. We shall display each of the person as an item in ListView as shown in the below format.

Name (MessageCount)

Also, we shall change the background color of each item in the list based on the message count.

To recreate this example, create a basic Flutter Application and replace main.dart with the following code.

main.dart

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatefulWidget {
  @override
  _State createState() => _State();
}

class _State extends State<MyApp> {
  final List<String> names = <String>['Aby', 'Aish', 'Ayan', 'Ben', 'Bob', 'Charlie', 'Cook', 'Carline'];
  final List<int> msgCount = <int>[2, 0, 10, 6, 52, 4, 0, 2];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Tutorial - googleflutter.com'),
      ),
      body: ListView.builder(
        padding: const EdgeInsets.all(8),
        itemCount: names.length,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            height: 50,
            margin: EdgeInsets.all(2),
            color: msgCount[index]>=10? Colors.blue[400]:
              msgCount[index]>3? Colors.blue[100]: Colors.grey,
            child: Center(
              child: Text('${names[index]} (${msgCount[index]})',
                style: TextStyle(fontSize: 18),
              )
            ),
          );
        }
      )
    );
  }
}

When you run this application in your Android Device or emulator, you should get a ListView as shown in the following screenshot.

Summary

In this Flutter Tutorial, we learned how to build a dynamic ListView from List of items using ListView.builder().

Desclaimer: We are not affiliated, associated, authorized, endorsed by, or in any way officially connected with the Google, Apple or Flutter, or any of its subsidiaries or its affiliates. The names Google, Apple and Flutter as well as related names, marks, emblems and images are registered trademarks of their respective owners. This site googleflutter.com covers tutorials related to Flutter developed by Google.