Dart Lists

Dart Lists

Dart List is similar to an array, which is the ordered collection of the objects. The array is the most popular and commonly used collection in any other programming language. The Dart list looks like the JavaScript array literals. The syntax of declaring the list is given below.

  1. var list1 = [101520,25,25]  

The Dart list is defined by storing all elements inside the square bracket ([]) and separated by commas (,).

Let's understand the graphical representation of the list -

Dart Lists

list1 - It is the list variable that refers to the list object.

Index - Each element has its index number that tells the element position in the list. The index number is used to access the particular element from the list, such as list_name[index]. The list indexing starts from 0 to length-1 where length denotes the numbers of the element present in the list. For example, - The length of the above list is 4.

Elements - The List elements refers to the actual value or dart object stored in the given list.

Types of Lists

The Dart

list can be categorized into two types -

  • Fixed Length List
  • Growable List

Fixed Length List

The fixed-length lists are defined with the specified length. We cannot change the size at runtime. The syntax is given below.

Syntax - Create the list of fixed-size

  1. var list_name = new List(size)  

The above syntax is used to create the list of the fixed size. We cannot add or delete an element at runtime. It will throw an exception if any try to modify its size.

The syntax of initializing the fixed-size list element is given below.

Syntax - Initialize the fixed size list element

  1. list_name[index] = value;  

Let's understand the following example.

Example -

  1. void main() {   
  2.    var list1 = new List(5);   
  3.    list1[0] = 10;   
  4.    list1[1] = 11;   
  5.    list1[2] = 12;   
  6.    list1[3] = 13;  
  7.    list1[4] = 14;    
  8.    print(list1);   
  9. }  

Output:

[10, 11, 12, 13, 14]

Explaination -

In the above example, we have created a variable list1 that refers the list of fixed size. The size of the list is five and we inserted the elements corresponding to its index position where 0th index holds 10, 1st index holds 12, and so on.

Growable List

The list is declared without specifying size is known as a Growable list. The size of the Growable list can be modified at the runtime. The syntax of the declaring Growable list is given below.

Syntax - Declaring a List

  1. // creates a list with values  
  2. var list_name = [val1, val2, val3]  
  3. Or   
  4. // creates a list of the size zero  
  5. var list_name = new List()  

Syntax - Initializing a List

  1. list_name[index] = value;  

Consider the following example -

Example - 1

  1. void main() {   
  2.    var list1 = [10,11,12,13,14,15];  
  3.    print(list1);   
  4. }  

Output:

[10, 11, 12, 13, 14, 15]

In the following example, we are creating a list using the empty list or List() constructor. The add() method is used to add element dynamically in the given list.

Example - 2

  1. void main() {   
  2.    var list1 = new List();   
  3.    list1.add(10);   
  4.    list1.add(11);   
  5.    list1.add(12);   
  6.    list1.add(13);  
  7.    print(list1);   
  8. }  

Output:

[10, 11, 12, 13]

List Properties

Below are the properties of the list.

PropertyDescription
firstIt returns the first element case.
isEmptyIt returns true if the list is empty.
isNotEmptyIt returns true if the list has at least one element.
lengthIt returns the length of the list.
lastIt returns the last element of the list.
reversedIt returns a list in reverse order.
SingleIt checks if the list has only one element and returns it.

Inserting Element into List

Dart provides four methods which are used to insert the elements into the lists. These methods are given below.

  • add()
  • addAll()
  • insert()
  • insertAll()

The add() Method

This method is used to insert the specified value at the end of the list. It can add one element at a time and returns the modified list object. Let's understand the following example -

Syntax -

  1. list_name.add(element);  

Example -

  1. void main() {  
  2.     var odd_list = [1,3,5,7,9];  
  3.     print(odd_list);  
  4.     odd_list.add(11);  
  5.     print(odd_list);  
  6. }   

Output:

[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9, 11]

Explanation -

In the above example, we have a list named odd_list, which holds odd numbers. We inserted a new element 11 using add() function. The add() function appended the element at the end of the list and returned the modified list.

The addAll() Method

This method is used to insert the multiple values to the given list. Each value is separated by the commas and enclosed with a square bracket ([]). The syntax is given below.

Syntax -

  1. list_name.addAll([val1,val2,val3,?..valN]);  

Let's understand the following example -

  1. void main() {  
  2.     var odd_list = [1,3,5,7,9]  
  3.      print(odd_list);  
  4.       odd_list.addAll([11,13,14]);  
  5.       print(odd_list);  
  6. }  

Output:

[1, 3, 5, 7, 9]
[1, 3, 5, 7, 9, 11, 13, 14]

Explaination -

In the above example, we don't need to call the add() function multiple times. The addAll() appended the multiple values at once and returned the modified list object.

The insert() Method

The insert() method provides the facility to insert an element at specified index position. We can specify the index position for the value to be inserted in the list. The syntax is given below.

  1. list_name.insert(index,value);  

Let's understand the following example -

  1. void main(){  
  2.     List lst = [3,4,2,5];  
  3.     print(lst);  
  4.     lst.insert(2,10);  
  5.     print(lst);  
  6. }  

Output:

[3, 4, 2, 5]
[3, 4, 10, 2, 5]

Explanation -

In the above example, we have a list of the random numbers. We called the insert() function and passed the index 2nd value 10 as an argument. It appended the value at the 2nd index and returned the modified list object.

The insertAll() Method

The insertAll() function is used to insert the multiple value at the specified index position. It accepts index position and list of values as an argument. The syntax is given below.

Syntax -

  1. list_name.insertAll(index, iterable_list_of_value)  

Let's understand the following example -

Example -

  1. void main(){  
  2.     List lst = [3,4,2,5];  
  3.      print(lst);  
  4.      lst.insertAll(0,[6,7,10,9]);  
  5.      print(lst);  
  6. }  

Output:

[3, 4, 2, 5]
[6, 7, 10, 9, 3, 4, 2, 5]

Explanation -

In the above example, we have appended the list of values at the 0th index position using the insertAll() function. It returned the modified list object.

Updating List

The Dart provides the facility to update the list and we can modify the list by simply accessing its element and assign it a new value. The syntax is given below.

Syntax -

  1. list_name[index] = new_value;  

Let's understand the following example -

Example -

  1. void main(){  
  2.       var list1 = [10,15,20,25,30];  
  3.       print("List before updation: ${list1}");  
  4.       list1[3] = 55;  
  5.       print("List after updation:${list1}");  
  6. }  

Output:

List before updation: [10, 15, 20, 25, 30]
List after updation: [10, 15, 20, 55, 30]

Explanation -

In the above example, we have accessed the 3rd index and assigned the new value 55 then printed the result. The previous list updated with the new value 55.

replaceRange() - The Dart provides replaceRange() function which is used to update within the given range of list items. It updates the value of the elements with the specified range. The syntax is given below.

Syntax -

  1. list_name.replaceRange(int start_val, int end_val, iterable);  

Let's understand the following example -

Example -

  1. void main(){  
  2.       var list1 = [10,15,20,25,30];  
  3.       print("List before updation: ${list1}");  
  4.       list1.replaceRange(0,4,[1,2,3,4]) ;  
  5.       print("List after updation using replaceAll() function : ${list1}");  
  6. }  

Output:

List before updation: [10, 15, 20, 25, 30]
List after updation using replaceAll() function : [1, 2, 3, 4, 30]

Explanation -

In the above example, we called the replaceRange() to the list which accepts the three arguments. We passed the starting index 0th, end index 4 and the list of the elements to be replaced as a third arguments. It returned the new list with the replaced element from the given range.

Removing List Elements

The Dart provides following functions to remove the list elements.

  • remove()
  • removeAt()
  • removeLast()
  • removeRange()

The remove() Method

It removes one element at a time from the given list. It accepts element as an argument. It removes the first occurrence of the specified element in the list if there are multiple same elements. The syntax is given below.

Syntax -

  1. list_name.remove(value)  

Let's understand the following example -

Example -

  1. void main(){  
  2.       var list1 = [10,15,20,25,30];  
  3.       print("List before remove element : ${list1}");  
  4.       list1.remove(20) ;  
  5.       print("List after removing element : ${list1}");  
  6. }  

Output:

List before remove element : [10, 15, 20, 25, 30]
List after removing element : [10, 15, 25, 30]

Explanation -

In the above example, we called the remove() function to the list and passed the value 20 as an argument. It removed the 20 from the given list and returned the new modified list.

The removeAt() Method

It removes an element from the specified index position and returns it. The syntax is given below.

Syntax -

  1. list_name.removeAt(int index)  

Example -

  1. void main(){  
  2.       var list1 = [10,11,12,13,14];  
  3.       print("List before remove element : ${list1}");  
  4.       list1.removeAt(3) ;  
  5.       print("List after removing element : ${list1}");  
  6. }  

Output:

List before remove element : [10, 11, 12, 13, 14]
List after removing element : [10, 11, 12, 14]

Explanation -

In the above example, we passed the 3rd index position as an argument to the removeAt() function and it removed the element 13 from the list.

The removeLast() Method

The removeLast() method is used to remove the last element from the given list. The syntax is given below.

Syntax-

  1. list_name.removeLast()  

Let's understand the following example.

Example -

  1. void main(){  
  2.      var list1 = [12,34,65,76,80];  
  3.      print("List before removing element:${list1}");  
  4.      list1.removeLast();  
  5.      print("List after removed element:${list1}");  
  6.   
  7. }  

Output:

List before removing element:[12, 34, 65, 76, 80]
List after removed element:[12, 34, 65, 76]

In the above example, we called the removeLast() method, which removed and returned the last element 80 from the given list.

The removeRange() Method

This method removes the item within the specified range. It accepts two arguments - start index and end index. It eliminates all element which lies in between the specified range. The syntax is given below.

Syntax -

  1. list_name. removeRange();  

Example -

  1. void main(){  
  2.      var list1 = [12,34,65,76,80];  
  3.      print("List before removing element:${list1}");  
  4.      list1.removeRange(1,3);  
  5.      print("List after removed element:${list1}");  
  6. }  

Output:

List before removing element:[12, 34, 65, 76, 80]
List after removed element:[12, 76, 80]

Explanation -

In the above example, we called the removeRange() method and passed start index position 1 and end index position 3 as an arguments. It removed all elements which were belonging in between the specified position.

Dart Iterating List elements

The Dart List can be iterated using the forEach method. Let's have a look at the following example.

Example -

  1. void main(){  
  2.      var list1 = ["Smith","Peter","Handscomb","Devansh","Cruise"];  
  3.      print("Iterating the List Element");  
  4.      list1.forEach((item){  
  5.      print("${list1.indexOf(item)}: $item");  
  6.  });  
  7. }  

Output:

Iterating the List Element
0: Smith
1: Peter
2: Handscomb
3: Devansh
4: Cruise

Note - We will learn forEach method in our loop in Dart section.


Comments