The `firstWhere` method in Dart returns the first element that satisfies the given predicate.
The syntax of Runes.firstWhere() method is:
int firstWhere(bool test(int element), { int orElse() })
This firstWhere() method of Runes returns the first element that satisfies the given predicate test
.
Parameter | Optional/Required | Description |
---|---|---|
test | required | A function that takes an integer element as input and returns a boolean value indicating whether the element satisfies the test. |
orElse | optional | A function that returns an integer value to be used if no element satisfies the test. |
Runes.firstWhere() returns value of type int
.
In this example,
runes
from the string 'Hello'.firstWhere
method with a test function to find the first ASCII character.void main() {
Runes runes = Runes('Hello');
int firstAscii = runes.firstWhere((element) => element >= 65 && element <= 90);
print('First ASCII character: $firstAscii');
}
First ASCII character: 72
In this example,
emojis
from the string '👋🏽🚀🌟'.firstWhere
method with a test function to find the first emoji character.void main() {
Runes emojis = Runes('👋🏽🚀🌟');
int firstEmoji = emojis.firstWhere((element) => element >= 128000);
print('First emoji: $firstEmoji');
}
First emoji: 128075
In this example,
numbers
from the string '12345'.firstWhere
method with a test function to find the first digit character.void main() {
Runes numbers = Runes('12345');
int firstDigit = numbers.firstWhere((element) => element >= 48 && element <= 57);
print('First digit: $firstDigit');
}
First digit: 49
In this Dart tutorial, we learned about firstWhere() method of Runes: the syntax and few working examples with output and detailed explanation for each example.