Creates a fixed-length list of the given length, and initializes the value at each position with fill
:
new List<int>.filled(3, 0); // [0, 0, 0]
The length
must be a non-negative integer.
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 entries in the returned list point to the same provided fill
value. That all items in the list are the same object is observable when the given value is a mutable object.
var shared = new List.filled(3, []); shared[0].add(499); print(shared); // => [[499], [499], [499]]
You may use List.generate to create a new object for each position in in the list.
var unique = new List.generate(3, (_) => []); unique[0].add(499); print(unique); // => [[499], [], []]
external factory List.filled(int length, E fill, {bool growable: false});
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/2.0.0/dart-core/List/List.filled.html