The `where` method in Dart returns a new lazy Iterable with all elements that satisfy the predicate test.
The syntax of Runes.where() method is:
Iterable<int> where(bool test(int element))
This where() method of Runes returns a new lazy Iterable with all elements that satisfy the predicate test
.
Parameter | Optional/Required | Description |
---|---|---|
test | required | A function that takes an integer element and returns true if the element satisfies the condition. |
Runes.where() returns value of type Iterable<int>
.
In this example,
numbers
containing integers.where()
method with a function that filters even numbers.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
Iterable<int> evenNumbers = numbers.where((element) => element % 2 == 0);
print(evenNumbers.toList());
}
[2, 4]
In this example,
words
containing strings.where()
method with a function that filters words with length greater than 5.void main() {
List<String> words = ['apple', 'banana', 'cherry'];
Iterable<String> longWords = words.where((word) => word.length > 5);
print(longWords.toList());
}
[banana, cherry]
In this example,
uniqueNumbers
containing integers.where()
method with a function that filters numbers greater than 1.void main() {
Set<int> uniqueNumbers = {1, 2, 3};
Iterable<int> filteredNumbers = uniqueNumbers.where((number) => number > 1);
print(filteredNumbers.toList());
}
[2, 3]
In this Dart tutorial, we learned about where() method of Runes: the syntax and few working examples with output and detailed explanation for each example.