- Dart Programming - Home
- Dart Programming - Overview
- Dart Programming - Environment
- Dart Programming - Syntax
- Dart Programming - Data Types
- Dart Programming - Variables
- Dart Programming - Operators
- Dart Programming - Loops
- Dart Programming - Decision Making
- Dart Programming - Numbers
- Dart Programming - String
- Dart Programming - Boolean
- Dart Programming - Lists
- Dart Programming - List Operations
- Dart Programming - Map
- Dart Programming - Symbol
- Dart Programming - Runes
- Dart Programming - Enumeration
- Dart Programming - Functions
- Dart Programming - Interfaces
- Dart Programming - Classes
- Dart Programming - Object
- Dart Programming - Collection
- Dart Programming - Generics
- Dart Programming - Packages
- Dart Programming - Exceptions
- Dart Programming - Debugging
- Dart Programming - Typedef
- Dart Programming - Libraries
- Dart Programming - Async
- Dart Programming - Concurrency
- Dart Programming - Unit Testing
- Dart Programming - HTML DOM
Dart Programming Useful Resources
Dart Programming - Removing Item from a List
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.
Example
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}');
}
Output
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.
Example
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}');
}
Output
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()
Example
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}');
}
Output
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.
Example
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}');
}
Output
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]