W3cubDocs

/Dart 2

last property

Future<T> last

The last element of this stream.

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

If this stream is empty (the done event is the first event), the returned future completes with an error.

Implementation

Future<T> get last {
  _Future<T> future = new _Future<T>();
  T result;
  bool foundResult = false;
  listen(
      (T value) {
        foundResult = true;
        result = value;
      },
      onError: future._completeError,
      onDone: () {
        if (foundResult) {
          future._complete(result);
          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.dart.dev/stable/2.5.0/dart-async/Stream/last.html