List<E>.filled constructor

List<E>.filled(
  1. int length,
  2. E fill,
  3. {bool growable: false}
)

Creates a list of the given length with fill at each position.

The length must be a non-negative integer.

Example:

new List<int>.filled(3, 0, growable: true); // [0, 0, 0]

The created list is fixed-length if growable is false (the default) and growable if growable is true. If the list is growable, changing its length will not initialize new entries with fill. After being created and filled, the list is no different from any other growable or fixed-length list created using List.

All elements of the returned list share the same fill value.

var shared = new List.filled(3, []);
shared[0].add(499);
print(shared);  // => [[499], [499], [499]]

You can use List.generate to create a list with a new object at each position.

var unique = new List.generate(3, (_) => []);
unique[0].add(499);
print(unique); // => [[499], [], []]

Implementation

external factory List.filled(int length, E fill, {bool growable = false});