The syntax of List.List.from constructor is:
List.from(Iterable elements, {bool growable = true})
This List.from constructor of List creates a list containing all elements
.
Parameter | Optional/Required | Description |
---|---|---|
elements | required | the elements used to create the list |
growable | optional [default value is true] | whether the list is growable |
In this example,
numbers
by calling List.from()
with elements [1, 2, 3]
.growable
parameter is provided, the list is growable by default.void main() {
List<int> numbers = List.from([1, 2, 3]);
print(numbers); // Output: [1, 2, 3]
}
[1, 2, 3]
In this example,
characters
by calling List.from()
with elements ['a', 'b', 'c']
and setting growable
parameter to false
.void main() {
List<String> characters = List.from(['a', 'b', 'c'], growable: false);
print(characters); // Output: [a, b, c]
}
[a, b, c]
In this example,
strings
by calling List.from()
with elements ['apple', 'banana', 'cherry']
.growable
parameter is provided, the list is growable by default.void main() {
List<String> strings = List.from(['apple', 'banana', 'cherry']);
print(strings); // Output: [apple, banana, cherry]
}
[apple, banana, cherry]
In this Dart tutorial, we learned about List.from constructor of List: the syntax and few working examples with output and detailed explanation for each example.