Dart predicate
last modified January 9, 2023
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() { var nums = <int>[0, -1, -2, -4, 5, 3, 6, -8]; var 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.
var 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() { var nums = <int>[0, -1, -2, -4, 5, 3, 6, -8]; var 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.
var 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() { var 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 removeAll
The removeWhere
function removes all elements from a collection
that satisfy the given predicate.
void main() { var 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() { var countries = <Country>[ new Country("Iran", 80840713), new Country("Hungary", 9845000), new Country("Poland", 38485000), new Country("India", 1342512000), new Country("Latvia", 1978000), new Country("Vietnam", 95261000), new Country("Sweden", 9967000), new Country("Iceland", 337600), new Country("Israel", 8622000) ]; var 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.
var 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)
In this article, we have covered predicates in Dart.
List all Dart tutorials.