Search code snippets, questions, articles...

Delete item from a List in Flutter [Dart]

Lists are mutable collections in Flutter or Dart. If you are using Lists then you can add or remove items from them using Dart's in-built function. In this post, we are going to learn different methods that can be used to remove one or multiple elements f
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

let's understand it one by one using code examples.

Remove item from a List [Condition based] using removeWhere() function

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:

  1. We have created a list called numbers that contain multiple integer type values.
  2. Using the removeWhere() function we are removing element 20 from the list and assigning it to "result" list variable.
  3. Print the result.
If you get the error - Error: This expression has type 'void' and can't be used, after running List.removeWhere() function, you can use the List..removeWhere() as shown in below 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.

Remove item at a specific position using removeAt() function

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]

Remove item [First Occurrence] from the List using remove() function

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]

Delete the last item from a list using the removeLast() function

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]

Delete items from a list using List.length

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.

Remove a range of items 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]
Was this helpful?
0 Comments