DartのfirstWhere

Posted by on Tue, Nov 27, 2018

下記のような感じで書いていて、条件に一致しない場合は、

members.firstWhere((member) => _lastMessage.fromUserId == member.userId);

Bad state: No element

と出ます。

仕様にも書いてました。

  /**
   * Returns the first element that satisfies the given predicate [test].
   *
   * Iterates through elements and returns the first to satisfy [test].
   *
   * If no element satisfies [test], the result of invoking the [orElse]
   * function is returned.
   * If [orElse] is omitted, it defaults to throwing a [StateError].
   */
  E firstWhere(bool test(E element), {E orElse()}) {
    for (E element in this) {
      if (test(element)) return element;
    }
    if (orElse != null) return orElse();
    throw IterableElementError.noElement();
  }


comments powered by Disqus