single property Null safety

Future<T> single

The single element of this stream.

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

If this is empty or has more than one element, the returned future completes with an error.

Implementation

Future<T> get single {
  _Future<T> future = new _Future<T>();
  late T result;
  bool foundResult = false;
  StreamSubscription<T> subscription =
      this.listen(null, onError: future._completeError, onDone: () {
    if (foundResult) {
      future._complete(result);
      return;
    }
    try {
      throw IterableElementError.noElement();
    } catch (e, s) {
      _completeWithErrorCallback(future, e, s);
    }
  }, cancelOnError: true);
  subscription.onData((T value) {
    if (foundResult) {
      // This is the second element we get.
      try {
        throw IterableElementError.tooMany();
      } catch (e, s) {
        _cancelAndErrorWithReplacement(subscription, future, e, s);
      }
      return;
    }
    foundResult = true;
    result = value;
  });
  return future;
}