The syntax of List.List.filled constructor is:
List.filled(int length, E fill, {bool growable = false})
This List.filled constructor of List creates a list of the given length with fill
at each position.
Parameter | Optional/Required | Description |
---|---|---|
length | required | the length of the list to be created |
fill | required | the value to be filled at each position in the list |
growable | optional [default value is false] | whether the list is allowed to grow |
In this example,
numbers
using List.filled
with a length of 5 and filling each position with 0.numbers
list to standard output.void main() {
List<int> numbers = List.filled(5, 0);
print(numbers);
}
[0, 0, 0, 0, 0]
In this example,
characters
using List.filled
with a length of 3 and filling each position with the character 'a'.characters
list to standard output.void main() {
List<String> characters = List.filled(3, 'a');
print(characters);
}
[a, a, a]
In this example,
flags
using List.filled
with a length of 4, filling each position with true
, and setting growable
to true
.flags
list to standard output.void main() {
List<bool> flags = List.filled(4, true, growable: true);
print(flags);
}
[true, true, true, true]
In this Dart tutorial, we learned about List.filled constructor of List: the syntax and few working examples with output and detailed explanation for each example.