The `singleWhere` method in Dart returns the single element that satisfies the given predicate, or throws an error if none or more than one element matches the predicate.
The syntax of Runes.singleWhere() method is:
int singleWhere(bool test(int element), { int orElse() })
This singleWhere() method of Runes returns the single element that satisfies 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 or more than one element satisfies the test. |
Runes.singleWhere() returns value of type int
.
In this example,
runes
from the string 'Hello'.singleWhere
method with a test function to find the single character code for 'H' (Unicode code point 72).void main() {
Runes runes = Runes('Hello');
int characterCode = runes.singleWhere((element) => element == 72);
print('Character code: $characterCode');
}
Character code: 72
In this example,
numbers
from the string '12345'.singleWhere
method with a test function to find the single digit '2' (Unicode code point 50).void main() {
Runes numbers = Runes('12345');
int digit = numbers.singleWhere((element) => element == 50);
print('Digit: $digit');
}
Digit: 50
In this Dart tutorial, we learned about singleWhere() method of Runes: the syntax and few working examples with output and detailed explanation for each example.