Dart| Flutter How to: multiple ways to Remove an element or object from a List with Example

This tutorial shows you multiple ways to remove elements from a list with examples in dart

Dart List remove element examples

List class inbuilt methods

  • remove method

  • removeAt method

  • removeWhere method

  • use remove method:

List class has a remove method, that removes elements with given input elements. It decreases the size of the list.

bool remove(Object? value)

It accepts an element.

It removes an element, and returns true, if the element is removed from the list, else false is returned.

The below program does not check case sensitive. Here is an example program.

void main() {
  List wordsList = ['one', 'two', 'three'];
  print(wordsList.remove("five")); //false
  print(wordsList.remove("One")); //false
  print(wordsList.remove("one")); //false

  print(wordsList);
}

Output:

false
false
true
[two, three]

Suppose, you have multiple elements(duplicate) matched in a list, It removes the first matched element from a list.

void main() {
  List wordsList = ['one', 'two', 'one','three'];
  print(wordsList.remove("one")); //false

  print(wordsList);
}

Output:

true
[two, one, three]
  • use removeAt method:

List class has the removeAt method, which removes elements with a given index. It decreases the size of the list.

dynamic removeAt(int index)

It accepts a valid index.

It removes an element at the index and returns an element if the element is removed from the list.

if it is an invalid index, It throws Uncaught Error: RangeError: Value not in the range: 5

an invalid index is a number that is greater than the list.length . Here is an example program.

void main() {
  List wordsList = ['one', 'two', 'three'];
  print(wordsList.removeAt(0)); //false

  print(wordsList);
  wordsList.removeAt(5); //Uncaught Error: RangeError: Value not in range: 5

  print(wordsList);
}


Output:

false
false
true
[two, three]

how to remove an object based on a key or property from a list?

This example explains about to deleting an object from a list of the object keys contains a condition.

In the below example,

  • Create an Employee object that contains id, name, salary,joinDate

  • Added employee object into List.

  • List contains the removeWhere method, which accepts a function

    void removeWhere(bool Function(Employee) test)
    

    The function is executed for every iteration

  • Remove the employee object whose id is equal to 21

    employees.removeWhere((element) => element.id == 21);
    

Here is an example program

class Employee {
  final int id;
  final String name;
  final int salary;
  final DateTime joinDate;

  Employee(this.id, this.name, this.salary, this.joinDate);
  @override
  String toString() {
    return '{id: ${id}, name: ${name}}';
  }

  Map<String, dynamic> toJson() =>
      {"id": id, "name": name, "salary": salary, "doj": joinDate.toString()};
}

void main() {
  final e1 =
      Employee(11, 'Erwin', 9000, DateTime.parse("2020-06-21 00:00:00.000"));
  final e2 =
      Employee(21, 'Andrew', 70000, DateTime.parse("2021-07-23 00:00:00.000"));
  final e3 =
      Employee(4, 'Mark', 8000, DateTime.parse("2017-08-24 00:00:00.000"));
  final e4 =
      Employee(12, 'Otroc', 5000, DateTime.parse("2022-12-05 00:00:00.000"));

  final List<Employee> employees = [e1, e2, e3, e4];
  print(employees); //92000

  employees.removeWhere((element) => element.id == 21);

  print(employees); //92000
}

Output:

[{id: 11, name: Erwin}, {id: 21, name: Andrew}, {id: 4, name: Mark}, {id: 12, name: Otroc}]
[{id: 11, name: Erwin}, {id: 4, name: Mark}, {id: 12, name: Otroc}]

Conclusion

To summarize, Learned multiple ways to remove primitive values and objects based on key values in flutter and dart with examples