The syntax of List.toList() method is:
List<E> toList({bool growable = true})
This toList() method of List creates a List
containing the elements of this Iterable
.
Parameter | Optional/Required | Description |
---|---|---|
growable | optional [default value is true] | whether the created list can grow dynamically or not |
List.toList() returns value of type List<E>
.
In this example,
iterable
containing integers.toList()
method on the iterable to convert it into a list.void main() {
Iterable<int> iterable = [1, 2, 3, 4, 5];
var list = iterable.toList();
print(list);
}
[1, 2, 3, 4, 5]
In this example,
iterable
containing characters.toList()
method on the iterable to convert it into a list.void main() {
Iterable<String> iterable = ['a', 'b', 'c'];
var list = iterable.toList();
print(list);
}
[a, b, c]
In this example,
iterable
containing strings.toList()
method on the iterable to convert it into a list.void main() {
Iterable<String> iterable = ['apple', 'banana', 'cherry'];
var list = iterable.toList();
print(list);
}
[apple, banana, cherry]
In this Dart tutorial, we learned about toList() method of List: the syntax and few working examples with output and detailed explanation for each example.