Dart Programming Tutorial on Dart Programming Lists

a very commonly used collection in programming is an array. dart represents arrays in the form of list objects. a list is simply an ordered group of objects. the dart:core library provides the list class that enables creation and manipulation of lists.

the logical representation of a list in dart is given below −

logical representation of a list
  • test_list − is the identifier that references the collection.

  • the list contains in it the values 12, 13, and 14. the memory blocks holding these values are known as elements.

  • each element in the list is identified by a unique number called the index. the index starts from zero and extends up to n-1 where n is the total number of elements in the list. the index is also referred to as the subscript.

lists can be classified as −

  • fixed length list
  • growable list

let us now discuss these two types of lists in detail.

fixed length list

a fixed length list’s length cannot change at runtime. the syntax for creating a fixed length list is as given below −

step 1 − declaring a list

the syntax for declaring a fixed length list is given below −

var list_name = new list(initial_size)

the above syntax creates a list of the specified size. the list cannot grow or shrink at runtime. any attempt to resize the list will result in an exception.

step 2 − initializing a list

the syntax for initializing a list is as given below −

lst_name[index] = value;

example

live demo
void main() { 
   var lst = new list(3); 
   lst[0] = 12; 
   lst[1] = 13; 
   lst[2] = 11; 
   print(lst); 
}

it will produce the following output

[12, 13, 11]

growable list

a growable list’s length can change at run-time. the syntax for declaring and initializing a growable list is as given below −

step 1 − declaring a list

var list_name = [val1,val2,val3]   
--- creates a list containing the specified values  
or  
var list_name = new list() 
--- creates a list of size zero 

step 2 − initializing a list

the index / subscript is used to reference the element that should be populated with a value. the syntax for initializing a list is as given below −

list_name[index] = value;

example

the following example shows how to create a list of 3 elements.

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

it will produce the following output

[1, 2, 3]

example

the following example creates a zero-length list using the empty list() constructor. the add() function in the list class is used to dynamically add elements to the list.

live demo
void main() { 
   var lst = new list(); 
   lst.add(12); 
   lst.add(13); 
   print(lst); 
} 

it will produce the following output

[12, 13] 

list properties

the following table lists some commonly used properties of the list class in the dart:core library.

sr.no methods & description
1 first

returns the first element in the list.

2 isempty

returns true if the collection has no elements.

3 isnotempty

returns true if the collection has at least one element.

4 length

returns the size of the list.

5 last

returns the last element in the list.

6 reversed

returns an iterable object containing the lists values in the reverse order.

7 single

checks if the list has only one element and returns it.