Lists in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

Lists in dart are an indexable collection of objects. These can contain objects of the same type as well as objects of different data type. It is also possible that we can create a list of fixed length or a list that is growable in nature.

Lists in dart are 0 index-based.

There are mainly two types of lists in dart. These mainly are −

  • Fixed Length List

  • Growable List

We will explore both types of lists in the following article.

Fixed Length List

A Fixed length list as the name suggest cannot grow. Also, it is not allowed to manipulate the size of it to something else.

Example

Consider the example shown below −

void main(){
   var fixedLengthList = List.filled(5, 3);
   print(fixedLengthList);
}

In the above code, we are telling the compiler that we want a list that will contain int data values and also that we want to fill five indexes with a default value '3' for all.

Output

[3, 3, 3, 3, 3]

We can also calculate the length of the list using the length property of the list class in dart.

Example

Consider the example shown below −

void main(){
   var fixedLengthList = List.filled(5, 3);
   print(fixedLengthList.length);
}

Output

5

Accessing and Changing the elements of the list is also possible. We access the list elements using the bracket notation and we also change the values at a particular index with the square bracket notation.

Example

Consider the example shown below −

void main(){
   var fixedLengthList = List.filled(5, 3);
   print(fixedLengthList[0]); // accessing the 0th index element
   fixedLengthList[0] = 99; // changing the element at 0 index
   print(fixedLengthList);
}

Output

3
[99, 3, 3, 3, 3]

It should be noted that if we try to insert any element into the list or in any way try to change the length of the list then the compiler will throw an error.

Example

Consider the example shown below −

void main(){
   var fixedLengthList = List.filled(5, 3);
   print(fixedLengthList.add(100));
   print(fixedLengthList);
}

Output

Error: This expression has type 'void' and can't be used.
   print(fixedLengthList.add(100));
                           ^
Error: Compilation failed.

Growable List

We can create a list that is growable by doing something like this −

Example

 Live Demo

void main() {
   var growableList = [1, 2, 3];
   print(growableList);
}

Output

[1, 2, 3]

We can access and change the values at a particular list index without any issue.

Example

Consider the example shown below -

 Live Demo

void main() {
   var growableList = [1, 2, 3];
   growableList[1] = 99;
   print(growableList[0]);
   print(growableList);
}

Output

1
[1, 99, 3]

It is also possible that we can add as many elements as we want, without having to run into any error as we did in a fixed-length list.

Example

Consider the example shown below −

 Live Demo

void main() {
   var growableList = [1, 2, 3];
   growableList.add(3);
   growableList.add(5);
   growableList.add(10);
   print(growableList);
}

Output

[1, 2, 3, 3, 5, 10]
raja
Published on 21-May-2021 13:04:06
Advertisements