Метод singleWhere
E singleWhere(Возвращает единственный элемент, который удовлетворяет test.
Проверяет все элементы, чтобы увидеть, возвращает ли test(element) значение true. Если ровно один элемент удовлетворяет test, возвращается этот элемент. В противном случае, если нет совпадающих элементов или если совпадающих элементов больше одного, выбрасывается StateError.
Исходный код
E singleWhere(bool test(E element)) {
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-core/Iterable/singleWhere.html