Dart Programming - Removing List items

The following functions supported by the List class in the dart:core library can be used to remove the item(s) in a List.
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.
List.remove(Object value)

Where,
value − represents the value of the item  that should be removed from the list.
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] 

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

Where,
index − represents the index of the element that should be removed from the list.
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] 

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] 

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.
Start − represents the starting position for removing the items.
End − represents the position in the list to stop 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]


    44 Lectures 
     4.5 hours 

 Sriyank Siddhartha

    34 Lectures 
     4 hours 

 Sriyank Siddhartha

    69 Lectures 
     4 hours 

 Frahaan Hussain

    117 Lectures 
     10 hours 

 Frahaan Hussain

    22 Lectures 
     1.5 hours 

 Pranjal Srivastava

    34 Lectures 
     3 hours 

 Pranjal Srivastava
 Print
 Add Notes
 Bookmark this page