lastWhere method

Future<T> lastWhere (bool test(T element), { T orElse() })

Finds the last element in this stream matching test.

If this stream emits an error, the returned future is completed with that error, and processing stops.

Otherwise as firstWhere, except that the last matching element is found instead of the first. That means that a non-error result cannot be provided before this stream is done.


Future<T> lastWhere(bool test(T element), {T orElse()}) {
  _Future<T> future = new _Future();
  T result;
  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) {
        if (orElse != null) {
          _runUserCode(orElse, future._complete, future._completeError);
        try {
          throw IterableElementError.noElement();
        } catch (e, s) {
          _completeWithErrorCallback(future, e, s);
      cancelOnError: true);
  return future;