by @kodeazy
flutter How to declare,initalize and add elements to a list in dart file?
- In this blog we will discuss on how to initalize and add elements in
List
. List
stores elements of similar type.-
Below is the syntax of declaration of
List
.List<E> exampleArray;
- Here
E
means type of variable likeint
orstring
oruserdefined class
. -
Before adding elements into
List
we initialize the list as below.exampleArray=[];
- Now after initializing the array to add elements we use
add
function. -
Below is the example syntax.
exampleArray.add(1);
-
Below is the sample program to declare,initialize and add elements into array.
void main() { List<int> exampleArray; // = [1, 2, 3]; exampleArray = []; exampleArray.add(1); exampleArray.add(2); exampleArray.add(3); print(exampleArray); }
Output:
[1, 2, 3]