Dart Programming - Removing List items


Advertisements

The following functions supported by the List class in the dart:core library can be used to remove the item(s) in a List.

List.remove()

The List.remove() function removes the first occurrence of the specified item in the list. This function returns true if the specified value is removed from the list.

Syntax

List.remove(Object value)

Where,

  • value − represents the value of the item 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}'); 
   bool res = l.remove(1); 
   print('The value of list after removing the list element ${l}'); 
}

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}'); 
} 

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}'); 
}

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}'); 
}

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]
dart_programming_lists_basic_operations.htm
Advertisements