Dart predicate tutorial shows how to use predicates in Dart. A predicate is a single argument function which returns a boolean value.
last modified January 28, 2024
In this article we show how to use predicates in Dart.
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.
The following is a simple predicate example.
main.dart
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)
The next example passes an anonymous predicate function to the where function.
main.dart
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)
The any function checks whether any element of the collection satisfies the given predicate.
main.dart
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
The removeWhere function removes all elements from a collection that satisfy the given predicate.
main.dart
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]
The next example uses a predicate with two conditions.
main.dart
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)
Dart List - language reference
In this article we have covered predicates in Dart.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Dart tutorials.