Introduction
In Flutter/Dart, we may need to initialize a List with values. In this article, we present several methods of how to do it.
How to initialize List with values in Flutter
In Flutter we can initialize a list simply using the []
operator:
The output:
To initialize a fixed-size list in Flutter we can use the filled()
constructor:
The output:
To initialize a growable list in Flutter we use the filled()
constructor with the growable: true
attribute:
The output:
To initialize a list in Flutter we can also use the from()
constructor that works similar to filled()
:
The output:
The difference between filled()
and from()
constructor is that filled()
by default creates fixed-size
list and from()
creates by default growable
list.
In Flutter there is also a constructor generate()
that can be used to create a list of items:
The output:
Conclusion
In this short article, we presented methods to initialize a list in Flutter/Dart.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}