Dart predicate
last modified January 28, 2024
In this article we show how to use predicates in Dart.
Predicate
Predicate in general meaning is a statement about something that is either true or false. In programming, predicates are single argument functions that return a boolean value.
Dart Predicate example
The following is a simple predicate example.
bool isPositive(int e) { return e > 0; } void main() { final nums = <int>[0, -1, -2, -4, 5, 3, 6, -8]; final filtered = nums.where((e) => isPositive(e)); print(filtered); }
In the program, the isPositive
predicate is used to filter out
positive values.
bool isPositive(int e) { return e > 0; }
The isPositive
predicate returns true for all values that are
bigger than zero.
final filtered = nums.where((e) => isPositive(e));
The predicate is passed to the where
function, which returns all
elements that satisfy the predicate.
$ dart main.dart (5, 3, 6)
Dart anonymous predicate
The next example passes an anonymous predicate function to the where function.
void main() { final nums = <int>[0, -1, -2, -4, 5, 3, 6, -8]; final filtered = nums.where((e) => e < 0); print(filtered); }
It is often not necessary to give a function a name. We can just pass an anonymous function.
final filtered = nums.where((e) => e < 0);
With the help of the anonymous function, we filter out all negative values.
$ dart main.dart (-1, -2, -4, -8)
Dart predicate with any
The any
function checks whether any element of the collection
satisfies the given predicate.
void main() { final nums = <int>[0, -1, -2, -4, 5, 3, 6, -8]; bool isAny = nums.any((e) => e > 0); if (isAny) { print("There is at least one positive value"); } else { print("There are no positive values"); } }
In the example, we find out if there are any positive values.
$ dart main.dart There is at least one positive value
Dart predicate with removeWhere
The removeWhere
function removes all elements from a collection
that satisfy the given predicate.
void main() { final words = <String>['sky', 'blue', 'cup', 'nice', 'top', 'cloud']; words.removeWhere((e) => e.length != 3); print(words); }
We have a list of strings. We remove all strings whose length is not 3.
$ dart main.dart [sky, cup, top]
Dart predicate multiple conditions
The next example uses a predicate with two conditions.
class Country { String name; int population; Country(this.name, this.population); String toString() { return "$name $population"; } } void main() { final countries = <Country>[ Country("Iran", 80840713), Country("Hungary", 9845000), Country("Poland", 38485000), Country("India", 1342512000), Country("Latvia", 1978000), Country("Vietnam", 95261000), Country("Sweden", 9967000), Country("Iceland", 337600), Country("Israel", 8622000) ]; final filtered = countries.where((e) => e.name.startsWith("I") && e.population > 10000000); print(filtered); }
We create a list of countries. We find all countries that start with 'I' and have population over one million.
final filtered = countries.where((e) => e.name.startsWith("I") && e.population > 10000000);
Two anonymous predicates are combined with the && operator.
$ dart main.dart (Iran 80840713, India 1342512000)
Source
Dart List - language reference
In this article we have covered predicates in Dart.
Author
List all Dart tutorials.