Debug School

rakesh kumar
rakesh kumar

Posted on

Dart Programming - Lists (Basic Operations)

Basic Operation & Description

  1. Inserting Elements into a List
  2. Updating a list
  3. Removing List items

Inserting Elements into a List

List.add() function appends the specified value to the end of the List and returns a modified List object

void main() { 
   List l = [1,2,3]; 
   l.add(12); 
   print(l); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

[1, 2, 3, 12]
The List.addAll() function accepts multiple values separated by a comma and appends these to the List.

void main() { 
   List l = [1,2,3]; 
   l.addAll([12,13]); 
   print(l); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

[1, 2, 3, 12, 13]
The List.addAll() function accepts multiple values separated by a comma and appends these to the List.

void main() { 
   List l = [1,2,3]; 
   l.addAll([12,13]); 
   print(l); 
} 
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

[1, 2, 3, 12, 13]

insert() function=> accepts a value and inserts it at the specified index.
insertAll()=> function inserts the given list of values, beginning from the index specified
List.insert(index,value)
List.insertAll(index, iterable_list_of _values)

The following examples illustrate the use of the insert() and insertAll() functions respectively.

Syntax
List.insert(index,value)
List.insertAll([Itearble])

Example : List.insert()

void main() { 
   List l = [1,2,3]; 
   l.insert(0,4); 
   print(l); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

[4, 1, 2, 3]
Example : List.insertAll()

void main() { 
   List l = [1,2,3]; 
   l.insertAll(0,[120,130]); 
   print(l); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

[120, 130, 1, 2, 3]

Updating the index

Dart allows modifying the value of an item in a List. In other words, one can re-write the value of list item

void main() { 
   List l = [1, 2, 3,]; 
   l[0] = 123;
   print (l);
}
Enter fullscreen mode Exit fullscreen mode

[123, 2, 3]
Using the List.replaceRange() function
The List class from the dart:core library provides the replaceRange() function to modify List items. This function replaces the value of the elements within the specified range.

The syntax for using List.replaceRange() function is as given below −

List.replaceRange(int start_index,int end_index,Iterable <items>)
Where,

Start_index − an integer representing the index position to start replacing.

End_index − an integer representing the index position to stop replacing.

− an iterable object that represents the updated values.

The following example illustrates the same −

void main() {
   List l = [1, 2, 3,4,5,6,7,8,9];
   print('The value of list before replacing ${l}');

   l.replaceRange(0,3,[11,23,24]);
   print('The value of list after replacing the items between the range [0-3] is ${l}');
}
Enter fullscreen mode Exit fullscreen mode

It should produce the following output −

The value of list before replacing [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of list after replacing the items between the range [0-3] is [11, 23, 24, 4, 5, 6, 7, 8, 9]

Dart Programming - Removing List items

Syntax
List.remove(Object value)

void main() { 
   List l = [1, 2, 3,4,5,6,7,8,9]; 
   print('The value of list before removing the list element ${l}'); 
   bool res = l.remove(1); 
   print('The value of list after removing the list element ${l}'); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

The value of list before removing the list element [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of list after removing the list element [2, 3, 4, 5, 6, 7, 8, 9]

List.removeAt()
The List.removeAt function removes the value at the specified index and returns it.

Syntax
List.removeAt(int index)
Where,

index − represents the index of the element that should be removed from the list.

The following example shows how to use this function −

void main() { 
   List l = [1, 2, 3,4,5,6,7,8,9]; 
   print('The value of list before removing the list element ${l}'); 
   dynamic res = l.removeAt(1); 
   print('The value of the element ${res}'); 
   print('The value of list after removing the list element ${l}'); 
} 
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

The value of list before removing the list element [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of the element 2
The value of list after removing the list element [1, 3, 4, 5, 6, 7, 8, 9]

List.removeLast()
The List.removeLast() function pops and returns the last item in the List. The syntax for the same is as given below −

List.removeLast()
The following example shows how to use this function −

void main() { 
   List l = [1, 2, 3,4,5,6,7,8,9]; 
   print('The value of list before removing the list element ${l}');  
   dynamic res = l.removeLast(); 
   print('The value of item popped ${res}'); 
   print('The value of list after removing the list element ${l}'); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

The value of list before removing the list element [1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of item popped 9
The value of list after removing the list element [1, 2, 3, 4, 5, 6, 7, 8]
List.removeRange()
The List.removeRange() function removes the items within the specified range. The syntax for the same is as given below −

List.removeRange(int start, int end)
Where,

Start − represents the starting position for removing the items.

End − represents the position in the list to stop removing the items.

The following example shows how to use this function −

void main() { 
   List l = [1, 2, 3,4,5,6,7,8,9]; 
   print('The value of list before removing the list element ${l}'); 
   l.removeRange(0,3); 
   print('The value of list after removing the list 
      element between the range 0-3 ${l}'); 
}
Enter fullscreen mode Exit fullscreen mode

It will produce the following output −

The value of list before removing the list element
[1, 2, 3, 4, 5, 6, 7, 8, 9]
The value of list after removing the list element
between the range 0-3 [4, 5, 6, 7, 8, 9]

Full Summary

  1. List.add()
  2. List.addAll()
  3. insert()
  4. insertAll()
  5. List.insert(index,value)
  6. List.insertAll(index, iterable_list_of _values)
  7. List.replaceRange()
  8. List.replaceRange(int start_index,int end_index,Iterable )
  9. List.remove(Object value)
  10. List.removeAt(int index)
  11. List.removeLast()
  12. List.removeRange(int start, int end)

Top comments (0)