Using List.filled() in Dart and Flutter (with examples)

Updated: September 11, 2023 By: A Goodman Post a comment

Overview

In Dart and Flutter, the List.filled() constructor is used to create a fixed-length list of the given length, and initialize the value at each position with a given fill value. This can be helpful when you need to create a list with a known size and a default value for each element.

Syntax:

List<E>.filled(int length, E fill, {bool growable = false})

Where E is the type of the elements in the list, length is the number of elements in the list, fill is the value to assign to each element, and growable is an optional parameter that determines whether the list can change its size later. The default value of growable is false, which means the list will have a fixed length. If growable is set to true, the list will be able to grow or shrink as needed.

The return value is a new list of type List<E>.

Examples

Below are some practical examples of how to use the List.filled() constructor in different scenarios.

Creating a list of zeros

Let’s say you want to create a list of 10 integers, all initialized to zero. You can use the List.filled() constructor with length set to 10 and fill set to 0. You don’t need to specify the growable parameter since you don’t plan to change the size of the list later.

// KindaCode.com
// main.dart
import 'package:flutter/foundation.dart';

void main() {
  // Create a list of 10 integers, all initialized to zero
  List<int> zeroList = List.filled(10, 0);

  // Print the list
  if (kDebugMode) {
    print(zeroList);
  }
}

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

If you try to add a new element to zeroList like so:

zeroList.add(0);

You’ll end up with this error:

Unhandled Exception: Unsupported operation: Cannot add to a fixed-length list

Creating a nested list that contains empty lists

In this example, we’ll create a list of 5 lists, each containing no elements. The solution here is to use the List.filled() constructor with length set to 5 and fill set to an empty list literal ([]). 

// KindaCode.com
// main.dart
import 'package:flutter/foundation.dart';

void main() {
  // Create a list of 5 lists, each containing no elements
  List<List<dynamic>> emptyList = List.filled(5, []);

  // Print the list
  if (kDebugMode) {
    print(emptyList);
  }
}

Output:

[[], [], [], [], []]

Creating a growable list of strings

Suppose you want to create a list of strings with an initial size of 3, but you want to be able to add or remove elements later. You can use the List.filled() constructor with length set to 3 and fill set to an empty string literal (""). You also need to specify the growable parameter as true, which means the list will be able to change its size later.

// KindaCode.com
// main.dart
import 'package:flutter/foundation.dart';

void main() {
  /// Create a growable list of strings with an initial size of 3
  List<String> stringList = List.filled(3, "", growable: true);

  if (kDebugMode) {
    // Print the list
    print(stringList);

    // Add an element at the end
    stringList.add("KindaCode.com");
    // Print the list
    print(stringList);

    // Remove an element at index 1
    stringList.removeAt(1);
    // Print the list
    print(stringList);
  }
}

Output:

[, , ]
[, , , KindaCode.com]
[, , KindaCode.com]

List.filled() vs List.generate()

The difference between List.filled() and List.generate() is that List.filled() creates a list with a single fill value for all elements, while List.generate() creates a list with a custom value for each element based on a generator function.

For instance, if you want to create a list of 10 integers, each initialized to its index squared, you can use List.generate() as follows:

List<int> squareList = List.generate(10, (int index) => index * index);

In general, List.generate() is more flexible than List.filled(), but in return, you have to define a generator function. You can find more details about List.generate() in this article: Using List.generate() in Flutter and Dart.

Conclusion

You have learned the fundamentals of the List.filled() constructor and have gone over some examples of utilizing it in practice. If you’d like to explore more new and exciting stuff in Dart and Flutter, take a look at the following articles:

You can also tour around our Flutter topic page or Dart topic page for the most recent tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles