Search code snippets, questions, articles...

Dart program for condition based item removal from a list

If you want to remove items from a list based on one or multiple conditions then you can use the .removeWhere() method of dart list.
void main() {
  List<Map<String, dynamic>> names = [
    { "id": 1, "name": "John" },
    { "id": 2, "name": "Rick" },
    { "id": 3, "name": "Daryl" },
    { "id": 4, "name": "Alex" }
  // Remove items if id is greater than or equals to 3
  names.removeWhere((item) => item['id'] >= 3);

If you want to remove items based on two conditions where the name is Rick and id is two then you can use the below syntax.

names.removeWhere((item) => item['id'] == 2 && item['name'] == 'Rick');
Was this helpful?
Programming Feeds
Learn something new everyday on Devsheet