let's understand it one by one using code examples.
The List.removeWhere() function takes the condition and removes the element from the list using value. You can put your condition function as a lambda function in the removeWhere() function and it will remove all the elements that meet the condition.
Code Example - Remove items from a flat list
List numbers = [10, 20, 20, 30, 20, 40, 50];
List result = numbers.removeWhere( (item) => item == 20 );
print(result);
Output
[10, 30, 40, 50]
In the above code example:
List numbers = [10, 20, 20, 30, 20, 40, 50];
List result = numbers..removeWhere( (item) => item == 20 );
print(result);
Code Example - Remove items from a list of sets
List data = [
{"id": 1, "name": "John"},
{"id": 2, "name": "Roy"},
{"id": 3, "name": "Carol"},
{"id": 4, "name": "Dave"}
];
List result = data.removeWhere( (item) => item["id"] == 3 );
print(result);
Output
[{id: 1, name: John}, {id: 2, name: Roy}, {id: 4, name: Dave}]
Here, we are removing the item which has an id value 3.
If you know the index of the item that you want to remove then you can use the in-built function of Dart List.removeAt() to remove the element from the list. It will take the index as a parameter and remove the item from that position.
Syntax
List.removeAt(index)
Code Example
// create a list
List numbers = [10, 20, 30, 40, 50];
//remove item from 2nd position
numbers.removeAt(2);
// print the list
print(numbers);
Output
[10, 20, 40, 50]
If you want to delete a single item from the list with its first occurrence in the list then you can use the List.remove() function. It will search for the first occurrence of the item in the list and delete it. The remove() function takes the item as a parameter and deletes it.
Syntax
List.remove(item)
Code Example
// create a list
List names = ["Sonal", "Shivam", "Parek", "Roy", "Tina"];
//remove item "Parek" from the list
names.remove("Parek");
// print the list
print(names);
Output
[Sonal, Shivam, Roy, Tina]
The List.removeLast() function can be used to delete the last item from a Dart List. You don't need to pass any parameter to this function. You can run it multiple times if you want to remove multiple last elements from the List.
Syntax
List.removeLast()
Code Example
// create a list
List names = ["Sonal", "Shivam", "Parek", "Roy", "Tina"];
//remove last item from the list
names.removeLast();
// print the list
print(names);
Output
[Sonal, Shivam, Parek, Roy]
We know that List.length is used to get the number of items in a Dart List. But do you know? It can also be used to remove items from the list. You can remove the last n items from the list using List.length. Let's understand it using the below code example.
Code Example
List numbers = [10, 20, 30, 40, 50];
int n = 3;
numbers.length = numbers.length - n;
print(numbers);
Output
[10, 20]
In the above code example, we are removing the last three elements from the list.
The List.removeRange() function is quite useful when you want to delete multiple elements from a list. It will take the start index and end index values as a parameter and removes the elements.
Syntax
List.removeRange(start_index, end_index)
Code Example
List numbers = [10, 20, 30, 40, 50, 60];
print("Input List: " + numbers.toString());
numbers.removeRange(2,5);
print("List after deletion: " + numbers.toString());
Output
Input List: [10, 20, 30, 40, 50, 60]
List after deletion: [10, 20, 60]
0 Comments