ZetCode

Dart filter List

last modified January 28, 2024

In this article we show how to filter List elements in Dart language.

A filtering operation processes a data structure (e.g. an array) and produces a new data structure containing exactly those elements for which the given predicate returns true.

A predicate is a single-argument function which returns a boolean value.

Dart filter List with where

The where function all elements that satisfy the predicate function.

main.dart
void main() {
  var vals = <int>[-1, 3, 2, 0, 1, -3, 4, 3, 5];
  var positive = vals.where((e) => e > 0);
  print(positive);

  var words = <String>["wolf", "sky", "falcon", "cloud", "wood", "oak"];
  var w3 = words.where((e) => e.length == 3);
  print(w3);
}

In the example, we filter two lists.

var vals = <int>[-1, 3, 2, 0, 1, -3, 4, 3, 5];
var positive = vals.where((e) => e > 0);

We filter out all elements that are positive. We pass an anonymous function to the where function.

var words = <String>["wolf", "sky", "falcon", "cloud", "wood", "oak"];
var w3 = words.where((e) => e.length == 3);

Here, we get all words whose lenght is 3.

$ dart main.dart 
(3, 2, 1, 4, 3, 5)
(sky, oak)

Dart filter List with retainWhere

The retainWhere function removes all elements from a list that fail to satisfy the given predicate.

main.dart
void main() {

  var words = <String>["wolf", "sky", "falcon", "cloud", "wood", "oak"];
  var words2 = List<String>.of(words);

  words2.retainWhere((e) => e.startsWith("w"));

  print(words);
  print(words2);
}

In the program, we want to take all words that start with 'w'.

var words = <String>["wolf", "sky", "falcon", "cloud", "wood", "oak"];
var words2 = List<String>.of(words);

First, we create a copy of the original list.

words2.retainWhere((e) => e.startsWith("w"));

We apply the retainWhere function on the list. It takes an anonymous predicate which checks if the element starts with 'w' with startsWith.

$ dart main.dart 
[wolf, sky, falcon, cloud, wood, oak]
[wolf, wood]

Dart removeWhere

The removeWhere removes all elements from a list that satisfy the given predicate.

main.dart
void main() {
  var words = <String?>["wolf", null, "falcon", null, "cloud", "wood", "oak"];

  words.removeWhere((e) => e == null);
  print(words); 
}

We remove all null values from the list of words.

$ dart main.dart 
[wolf, falcon, cloud, wood, oak]

Dart filter a List of objects

In the next example, we filter a list of objects.

main.dart
class Employee {
  String fname;
  String lname;
  int salary;

  Employee(this.fname, this.lname, this.salary);

  @override
  String toString() {
    return "$fname $lname: $salary";
  }
}

void main() {
  var empls = <Employee>[
    new Employee("John", "Doe", 1230),
    new Employee("Adam", "Novak", 670),
    new Employee("Robin", "Brown", 2300),
    new Employee("Rowan", "Cruise", 990),
    new Employee("Joe", "Draker", 1190),
    new Employee("Janet", "Doe", 980),
    new Employee("Lucy", "Smith", 980),
    new Employee("Thomas", "Moore", 1400)
  ];

  var filtered = empls.where((e) => e.salary > 1000);
  print(filtered);
}

We have a list of employees. We want all employees that have salary higher than 1000.

var filtered = empls.where((e) => e.salary > 1000);

In the predicate, we access the salary attribute of the element and compare it with the 1000 value.

$ dart main.dart 
(John Doe: 1230, Robin Brown: 2300, Joe Draker: 1190, Thomas Moore: 1400)

Dart whereType

With whereType function, we can get all values of the given type.

main.dart
void main() {
  var data = ['sky', 2, 'owl', 11, 'forest', 'falcon'];

  var words = data.whereType<String>();
  print(words);
}

In the example, we filter out all words from the data list.

$ dart main.dart 
(sky, owl, forest, falcon)

Dart List comprehension

A list comprehension is a syntactic construct which creates a list based on existing list.

main.dart
  var vals = <int>[-1, 3, 2, 0, 1, -3, 4, 3, 5];

  var negative = [
    for (var e in vals)
      if (e < 0) e
  ];

  print(negative);
}

Using list comprehension, we create a new list that contains only negative values.

$ dart main.dart 
[-1, -3]

Source

Dart List - language reference

In this article we have filtered Dart lists.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Dart tutorials.