On this page
singleWhere method
E singleWhere(Returns the single element that satisfies test
.
Checks all elements to see if test(element)
returns true. If exactly one element satisfies test
, that element is returned. Otherwise, if there are no matching elements, or if there is more than one matching element, a StateError
is thrown.
Source
E singleWhere(bool test(E element)) {
int length = this.length;
E match = null;
bool matchFound = false;
for (int i = 0; i < length; i++) {
E element = this[i];
if (test(element)) {
if (matchFound) {
throw IterableElementError.tooMany();
}
matchFound = true;
match = element;
}
if (length != this.length) {
throw new ConcurrentModificationError(this);
}
}
if (matchFound) return match;
throw IterableElementError.noElement();
}
© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-collection/ListMixin/singleWhere.html