-1.6 C
New York
Wednesday, February 1, 2023

How to remove items from a list in Dart

One of the most common tasks you encounter while working with a list in Dart is removing its items. Similar to various programming languages, Dart has some built-in functions that help us get this matter done quickly.

remove()

This method is used to remove the first occurrence of a value from a list. It returns true if that value was in the list, returns false otherwise.Advertisements

Example:

final myList = ['a', 'b', 'c', 'd', 'e', 'f', 'd', 'b'];
void main(){
  myList.remove('b');
  print(myList); 
}

Output:

[a, c, d, e, f, d, b]

removeWhere()

This method is used to remove all items from a list that satisfy one or many conditions.

Example

This example removes people who are under 18 from a given list:

final List<Map<String, dynamic>> people = [
  {'name': 'John', 'age': 30},
  {'name': 'Bob', 'age': 50},
  {'name': 'Tom', 'age': 14},
  {'name': 'Donald', 'age': 3}
];

void main() {
  people.removeWhere((person) => person['age'] <= 18);
  print(people);
}

Output:

[{name: John, age: 30}, {name: Bob, age: 50}]

removeAt()

Advertisements

This method is used to remove an item at position index from a list.

Example

This program remove the second person from a list of people:

final List<Map<String, dynamic>> people = [
  {'name': 'John', 'age': 30},
  {'name': 'Bob', 'age': 50},
  {'name': 'Tom', 'age': 14},
  {'name': 'Donald', 'age': 3}
];

void main() {
  people.removeAt(2);
  print(people);
}

Output:

[{name: John, age: 30}, {name: Bob, age: 50}, {name: Donald, age: 3}]

removeLast()

This method pops and returns the last item of a list.

Example

This example remove the last person from the list:

final List<Map<String, dynamic>> people = [
  {'name': 'John', 'age': 30},
  {'name': 'Bob', 'age': 50},
  {'name': 'Tom', 'age': 14},
  {'name': 'Donald', 'age': 3}
];

void main() {
  people.removeLast();
  print(people);
}

Output:

[{name: John, age: 30}, {name: Bob, age: 50}, {name: Tom, age: 14}]

removeRange()

This method removes the items in the range start inclusive to end exclusive:

void removeRange (int start, int end)

Example

Advertisements

This program removes elements whose indices are from 2 to 5:

final myList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];

void main(){
  myList.removeRange(2, 6);
  print(myList);
}

Output:

[a, b, g, h]

Conclusion

Congratulations! At this point, you have learned 5 different methods to remove items from a list in Dart and can begin using them to build complicated Flutter projects.

You can also read about swiping to remove items from a ListView – highlighting selected items in a ListView – implementing horizontal ListView – Flutter: ListView Pagination (Load More) example to get a better sense of the list and ListView in Flutter.

In order to see the latest tutorials and examples, take a look at our Flutter topic page and Dart topic page. Happy coding.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles