Spec-Zone.ru › Dart 1

dart:collection

Метод singleWhere

E singleWhere(bool test(E element))

Возвращает единственный элемент, который удовлетворяет test.

Проверяет все элементы, чтобы увидеть, возвращает ли test(element) true. Если ровно один элемент удовлетворяет test, этот элемент возвращается. В противном случае, если нет соответствующих элементов или если есть более одного соответствующего элемента, выбрасывается StateError.

Исходный код

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

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API