lastWhere method

Future lastWhere(bool test(T element), { Object defaultValue() })

Finds the last element in this stream matching test.

As firstWhere, except that the last matching element is found. That means that the result cannot be provided before this stream is done.

Source

Future<dynamic> lastWhere(bool test(T element), {Object defaultValue()}) {
  _Future<dynamic> future = new _Future();
  T result = null;
  bool foundResult = false;
  StreamSubscription subscription;
  subscription = this.listen(
      (T value) {
        _runUserCode(() => true == test(value), (bool isMatch) {
          if (isMatch) {
            foundResult = true;
            result = value;
          }
        }, _cancelAndErrorClosure(subscription, future));
      },
      onError: future._completeError,
      onDone: () {
        if (foundResult) {
          future._complete(result);
          return;
        }
        if (defaultValue != null) {
          _runUserCode(defaultValue, future._complete, future._completeError);
          return;
        }
        try {
          throw IterableElementError.noElement();
        } catch (e, s) {
          _completeWithErrorCallback(future, e, s);
        }
      },
      cancelOnError: true);
  return future;
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-async/Stream/lastWhere.html