To initialize a list in Dart, you can use the list literal syntax []
for an empty list or provide elements inside the brackets to create a list with initial values.
In this example,
[]
to create an empty list and assign it to the variable emptyList
.void main() {
List<int> emptyList = [];
print('Empty List: \$emptyList');
}
Empty List: []
In this example,
[]
and provide elements inside the brackets to create a list with initial values.numbers
.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
print('List with Elements: \$numbers');
}
List with Elements: [1, 2, 3, 4, 5]
In this tutorial, we learned How to Initialize a List in Dart language with well detailed examples.