Метод singleWhere
E singleWhere(Возвращает единственный элемент, который удовлетворяет test.
Проверяет все элементы, чтобы убедиться, что test(element) возвращает true. Если ровно один элемент удовлетворяет test, этот элемент возвращается. В противном случае, если нет совпадений или если совпадений больше одного, выбрасывается StateError.
Исходный код
E singleWhere(bool test(E value)) {
E result = null;
bool foundMatching = false;
for (E element in this) {
if (test(element)) {
if (foundMatching) {
throw IterableElementError.tooMany();
}
result = element;
foundMatching = true;
}
}
if (foundMatching) return result;
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/IterableMixin/singleWhere.html