Lists ( or Arrays in other programming languages) are one of the very important part of Dart programming language.

In its very basic form, a List can be created like this,

var myList = [1, 2, 3];

Since Dart can infer the data type to a variable, it will give List<int> type to the variable myList. Means, you cannot add any other value to this list other than int.

Dart also provides several properties and methods for List data type for most commonly used tasks.

Length:

The length property of a list, as the name suggests gives the length of that array. To the previous example it will be 3.

print(myList.length); // we get 3
Add:

The add method (this is similar to push in JavaScript) will let us add new elements to a list created like before (we cannot use add on certain types of lists which we will see later).

void main() {
  var myList = [1, 2, 3];
  myList.add(4);
  print(myList); // we get [1, 2, 3, 4]
}

Clear:

The clear method, clears the list.

void main() {
  var myList = [1, 2, 3];
  myList.clear();
  print(myList); // we get []
}

IndexOf:

IndexOf method returns the first index of the element which matches the value we pass to this method. We also have lastIndexOf method which returns the position of last occurrence.

If the value we pass to the method is not there in the list, then we get -1 (similar to JavaScript).

 var myList = [1, 2, 3, 2, 3];
  print(myList.indexOf(2)); // lists index starts with 0 so we get 1
  print(myList.indexOf(5)); // we get -1 

Shuffle:

This method shuffles the list items randomly.

var myList = [1, 2, 3, 2, 3];
  myList.shuffle();
  print(myList); //You may get a different value here since its a random shuffle, We got [3, 2, 1, 3, 2]

There are many more methods available on Dart which you can check here  (please pay attention to the version number. We linked to the version which is latest while writing this tutorial, there may be newer versions when you are reading this)

Types of Lists in Dart

We have created a List in its very basic form earlier. When we create a list like this, we can add items, delete items and do any type of operation on that list. But sometimes we may want to create lists that are un-modifiable or of fixed length etc., and for that same reason, Dart offers different types of lists.

Dart features two types of lists, Growable lists and Fixed length lists.

Growable List:

This is the list that we have created earlier, Growable. That means, the list can grow in its size (length) and can also shrink.

Earlier developers used to use syntax like this “var myList = List<int>() ;” but now it is deprecated.

Fixed Length List:

Fixed length lists are, as the name, are of fixed length. We cannot change its length.

One of the syntax to create fixed length list earlier was “var myList = List(3);“. This syntax is deprecated now. The reason being, if you create a list like this, by default Dart used to fill the list with specified number of null values. In-order to make way for non nullability,  the syntax was deprecated.

Now we can use couple of other ways to create lists with fixed length.

Filled:

We can use filled method of a List to create lists with fixed length and filled with specified values.

example:

var myList = List.filled(3, 0);
  print(myList); // we get [0, 0, 0]
  myList[0] = 100;
  myList[1] = 200;
  myList[2] = 300;
  print(myList); // we get [100, 200, 300]

In this example we can see that initially we created a list of length 3 and filled with 0. Later if we want, we can change values but we cannot add more elements or remove.

If we try to do mylist[3] = 400, then we get run time error “RangeError (RangeError (index): Invalid value: Not in inclusive range 0..2: 3)“.

If we try to do myList.add(400), then we get “UnsupportedError (Unsupported operation: Cannot add to a fixed-length list)” and a similar error if we try to use removeLast  or any other method which causes to alter the length of the list.

When we create a list by using filled, by default they are of fixed length. If we want to create Growable lists using filled, then we can pass a third argument to the filled method. Then we can use add, insert or other methods on that list.

example:

var myList = List.filled(3, 0, growable: true);
  print(myList); // we get [0, 0, 0]
  myList[0] = 100;
  myList[1] = 200;
  myList[2] = 300;
  // myList[3] = 400; // we cannot do it, 4th item is not available
  myList.insert(3, 400);
  myList.add(500);
  print(myList); // we get [100, 200, 300, 400, 500]
  myList.length = 2;
  print(myList); // we get [100, 200]
Generate:

Another way to create lists is by using the Generate method List class. This is also one of the widely used method in Flutter.

The lists created with filled method are by default of Fixed Length. The lists created with Generate are by default Growable.

void main() {
  var myList = List.generate(3, (index) => index + index);
  myList.add(100);
  print(myList); // we get [0, 2, 4, 100]
}

To the generate method, we pass the length as the first argument and a callback function as the second argument. The return value of the callback function for each iteration will be the value of the item in the list.

We can also pass a third argument to make a list of fixed length.

var myList = List.generate(3, (index) => index + index, growable: false);
  // myList.add(100); // we can't do this since the list is of fixed length
  print(myList); // we get [0, 2, 4]

Unmodifiable Lists

Dart allows us to create lists that are un-modifiable. Values of fixed length lists can be changed as we have seen earlier, but, we cannot even change the values of an unmodifiable list.

example

var myList = List.unmodifiable([1, 2, 3]);

  // myList[0] = 100; // we cannot modify the value, we get runtime error

When we try to modify a value, we get the runtime error “UnsupportedError (Unsupported operation: Cannot modify an unmodifiable list)“.

Empty

We can create an empty list in Dart by using the empty method of List class. By default this list will be of fixed length. If we want to make it growable, we need to pass an argument to the method while creating the list.

example:

var myEmptyList1 = List.empty();
  // myEmptyList1.add(100)// we get runtime error

  var myEmptyList2 = List.empty(growable: true);
  myEmptyList2.add(100);

  print(myEmptyList1); // we get []
  print(myEmptyList2); // we get [100]

Conditionally Adding List Items:

We can add elements to a list conditionally like this

void main() {
  var isLoggedIn = true;
  var myList = [1, 2, 3, if (isLoggedIn) 4];
  print(myList); // we get [1, 2, 3, 4]
}

We can also use ternary operators like this

void main() {
  var isLoggedIn = false;
  var myList = [1, 2, 3, isLoggedIn ? 4 : 0];
  print(myList); // we get [1, 2, 3, 0]
}

Using For loop to create a list:

We can also use loops to create a list.

void main() {
  var myList = [for (var i = 0; i < 5; i++) i + i];
  print(myList); // we get [0, 2, 4, 6, 8]
}