Dart .remove()
Anonymous contributor
Published Feb 23, 2024
Contribute to Docs
The .remove() method is used to remove the first occurrence of an item from a list. It returns true if the item is present and is successfully removed. if the item is not present, it returns false.
Note: The list must be a growable list.
Syntax
list.remove(value)
list: The name of the list to be modified.value: The value to be removed from the list.
Example
In the following example, the .remove() method is used to delete the first occurrence of the value 1 from the list variable:
void main() {List list = [2, 1, 3, 5, 1, 7];print('Original list : ${list}');bool retValue = list.remove(1);print('Return value of remove function : ${retValue}');print('New list : ${list}');retValue = list.remove(4);print('Return value of remove function : ${retValue}');print('New list : ${list}');}
The above code will produce the following output:
Original list : [2, 1, 3, 5, 1, 7]Return value of remove function : trueNew list : [2, 3, 5, 1, 7]Return value of remove function : falseNew list : [2, 3, 5, 1, 7]
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Dart on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours