List is one of the data structures in Dart Collections used to store the collection of values or objects. A list is an ordered list and allows duplicate elements.

The list is a Class of the dart: core package in the Dart language.

The list stores the elements in order of way

For example, the List is 12,4,5,1,8.

The list index always starts with zero and ends index with the list.length -1.

list[0] is 12, List[4] is 8.

The list contains two types List.

  • Fixed size List:Dart has no support for Array and it is equal to Array in Dart. List length does not allow you to change at runtime.

  • Growable List: There is no fixed length and the list is growable and length is changeable.

How do you create a List object in Dart?

Let’s see an example of how to create

  • Fixed List creation

List.filled() method used to create a list.

Syntax

List List.filled(int length, String fill, { bool growable = false, })

It creates a list with a length property and filled the list with the fill value. growable is a boolean value to indicate it is the fixed or growable list.

Here is an example for creating a fixed list in dart

void main() {
  var listvariable = new List<String>.filled(3, "", growable: false);

  listvariable[0] = "one";
  listvariable[1] = "two";
  listvariable[2] = "three";
  print(listvariable);
}

Output:

[one, two, three]

This is equal to an array in Dart.

In the above example, Create a list of length=3 with filled values empty.

each element is updated with an index starting from zero to 2 since the length is three.

It throws an error if you try to insert the fixed length with an index greater than or equal to the length.

Uncaught Error: RangeError (index): Index out of range: index should be less than 3: 4:

void main() {
  var listvariable = new List<String>.filled(3, "", growable: false);

  listvariable[0] = "one";
  listvariable[1] = "two";
  listvariable[2] = "three";
  listvariable[4] = "four";


}

Dart Growable List

Growable list size is not known at declaration and size can be changed at runtime

It can be created in the following ways.

  • use the filled method List.filled method allows you to fill the size of elements with a growable boolean value

Here is an example

void main() {
  var listvariable = new List<String>.filled(0, "", growable: true);

  listvariable = ["one", "two", "three"];
  print(listvariable);
}
  • Using the empty method with growable is true The List. empty() method allows you to create an empty list with a growable size.
void main() {
  List<String> listvariable = List<String>.empty(growable: true); // []

  listvariable = ["one", "two", "three"];
  print(listvariable);
}
  • use empty brackets

List variable is created with empty brackets also called empty list literal. list values are initialized during declaration or empty with list literal syntax.

void main() {
  var listvariable = [];
  var listvariable1 = [1, 2, 3];

  listvariable = ["one", "two", "three"];
  listvariable1.add(4);
  print(listvariable);
  print(listvariable1);
}

The list can be created with the default constructor in my previous versions of dart

We can not use new List(3) or new List() since the default constructor is deprecated.

void main() {
  var listvariable = new List();

  print(listvariable);
}

The above code throws an error Error: Can’t use the default List constructor.

Dart List properties

Following is the list of class properties.

For example, a List contains the following elements.

 var numbers= [1,3,4,5]

|List Property | Description | result| |:——–| ————— | |first| First element in a list| numbers.first is 1 | |last| last element in a list| numbers.last is 5 |

|length| Returns size of an list| numbers.length is 4 | |isEmpty| true - if list is empty,else false| numbers.isEmpty is false | |isNonEmpty| true - if list is not empty,else false| numbers.isNonEmpty is true | |reversed| List is reversed order| numbers.reversed is [5,4,3,1] | |Single| true if list contains single element,else false| numbers.Single is false |