Summary: in this tutorial, you’ll learn how to use the where()
method of an iterable object to filter a list.
Introduction to the Iterable<E> where() method
An iterable object is an ordered collection of elements. Dart uses the Iterable<E>
abstract class to represent iterable objects.
Both List
and Set
are iterable. Therefore, they implement all methods of the Iterable
class. All classes that implement the iterable
class have the where()
method.
The where()
method returns a new iterable with all elements that satisfy a test:
Iterable<E> where( bool test(E element));
Code language: Dart (dart)
In this syntax, the where()
method takes a function as an argument:
bool test(E element)
Code language: Dart (dart)
The where()
method iterates over all elements of an iterable object. If an element satisfies the test()
function, the where()
method will include it in the Iterable
result object.
In other words, any element that makes the test()
function true will be included in the result.
Filter a list example
Suppose you have a list of integers:
var numbers = [2, 1, 7, 4, 9];
Code language: Dart (dart)
To find all numbers that are greater than 5, you may use a for-in
loop like this:
void main() {
var numbers = [2, 1, 7, 4, 9];
List<int> results = [];
for (var number in numbers) {
if (number > 5) {
results.add(number);
}
}
print(results);
}
Code language: Dart (dart)
Output:
[2, 4, 6]
Code language: Dart (dart)
In this example, we use a for-in
loop to iterate over the elements of the numbers
list and add only numbers that are greater than 5 to the results
list.
To make it shorter, you can use the where()
method to filter the list:
void main() {
var numbers = [2, 1, 7, 4, 9];
var results = numbers.where((n) => n > 5);
print(results);
}
Code language: Dart (dart)
Output:
(7, 9)
Code language: Dart (dart)
The where()
method returns an iterable object. Therefore, you see the ()
instead of []
.
If you want to convert an iterable object to a list, you can use the toList()
method like this:
void main() {
var numbers = [2, 1, 7, 4, 9];
var results = numbers.where((n) => n > 5);
print(results.toList());
}
Code language: Dart (dart)
Output:
[7, 9]
Code language: Dart (dart)
Using the where() method to filter a list of objects example
The following example uses the where()
method to return a list of people whose ages are greater than or equal to 18:
class Person {
String name;
int age = 0;
Person({this.name = '', this.age = 0});
@override
String toString() => name;
}
void main() {
var people = [
Person(name: 'Alice', age: 18),
Person(name: 'Bob', age: 16),
Person(name: 'John', age: 21),
Person(name: 'Peter', age: 23),
Person(name: 'David', age: 15),
];
var users = people.where((p) => p.age >= 18);
print(users);
}
Code language: Dart (dart)
Output:
(Alice, John, Peter)
Code language: Dart (dart)
How it works.
First, define the Person
class with two properties name
and age
:
class Person {
String name;
int age = 0;
Person({this.name = '', this.age = 0});
@override
String toString() => name;
}
Code language: Dart (dart)
Second, declare a list of Person
objects:
var people = [
Person(name: 'Alice', age: 18),
Person(name: 'Bob', age: 16),
Person(name: 'John', age: 21),
Person(name: 'Peter', age: 23),
Person(name: 'David', age: 15),
];
Code language: Dart (dart)
Third, use the where()
method to filter the Person
objects:
var users = people.where((p) => p.age >= 18);
Code language: Dart (dart)
Finally, display the result:
print(users);
Code language: Dart (dart)
Summary
- Use the
where()
method to return an iterable of objects that satisfy a test.