thumbnail

Lists in Dart

Published on 29 March 2020
Last Updated on 29 March 2020

What are lists in Dart

Lists in Dart programming language are array like objects used for storing objects of specific data type in the form of a list.

In Dart programming language, arrays are a list object.

Lists are ordered group of objects.

Similar to arrays, index of list objects starts with 0.

Properties of list objects

  • Each objects stored in a list has a unique index.
  • List objects are stored sequentially in memory.
  • List object can be accessed using its index value.

List element index

Each element in the list is identified by a unique index. This index is also called as the element index.

For example:

// Program for demonstrating list indexes in Dart
main(List<String> args) {
  var a = ['b', 'b', 'c', 'd'];

  for (var i = 0; i < a.length; i++) {
    // accessing list items using their index value
    print('element at index ${i} is ${a[i]}');
  }
}

Above program produces following output:

element at index 0 is b
element at index 1 is b
element at index 2 is c
element at index 3 is d

Accessing a specific list element using its index

Refer to program given below to understand how you can access a specific element in a Dart list using its index value.

main(List<String> args) {
  var a = ['b', 'b', 'c', 'd'];

  print('element at index 2 is');
  print(a[2]);

  print('element at index 0 is');
  print(a.elementAt(0));
}

Above program produces following output:

element at index 2 is
c
element at index 0 is
b

Types of List

List objects can be of two types:

  1. Fixed length list
  2. Growable list

1. Fixed length list in Dart

A fixed length list is a list whose length does not change at runtime.

Given below is a program that shows how to define and use fixed length list.

main(List<String> args) {
  // creates a new list of alphabets with a fixed length
  var alphabets = new List<String>(3);
  alphabets[0] = 'a';
  alphabets[1] = 'b';
  alphabets[2] = 'c';
  for (var i = 0; i < alphabets.length; i++) {
    // accessing list items using their index value
    print('element at index ${i} is ${alphabets[i]}');
  }
}

Above program produces following output:

element at index 0 is a
element at index 1 is b
element at index 2 is c

Another way to define a fixed length list to use the const keyword.

main(List<String> args) {
  const listOfNumbers = [1, 2, 3];
  print('first element of the list of numbers is: ${listOfNumbers[0]}');

  // elements of const list can not be re-assigned, un-commenting following line will cause a runtime error
  // the error might look like:
  // Unhandled exception:
  // Unsupported operation: Cannot modify an unmodifiable list
  //
  // listOfNumbers[0] = 12;
}

Above program produces following output:

first element of the list of numbers is: 1

2. Growable list in Dart

A growable list, is a type of list whose length can change at run-time.

Given below is an example of list of growable elements list.

main(List<String> args) {
  var listOfAlphabets = ['a', 'b', 'c', 'd', 'e'];
  // dynamically adding new elements to the list
  listOfAlphabets.add('f');
  listOfAlphabets.add('g');
  listOfAlphabets.add('h');
  listOfAlphabets.add('i');
  for (var i = 0, l = listOfAlphabets.length; i < l; i++) {
    // accessing list items using their index value
    print('element at index ${i} is ${listOfAlphabets[i]}');
  }
}

Above program produces following output:

element at index 0 is a
element at index 1 is b
element at index 2 is c
element at index 3 is d
element at index 4 is e
element at index 5 is f
element at index 6 is g
element at index 7 is h
element at index 8 is i