Returns the first element of the stream.
Stops listening to the stream after the first element has been received.
Internally the method cancels its subscription after the first element. This means that single-subscription (non-broadcast) streams are closed and cannot be reused after a call to this getter.
If an error event occurs before the first data event, the resulting future is completed with that error.
If this stream is empty (a done event occurs before the first data event), the resulting future completes with a StateError.
Except for the type of the error, this method is equivalent to
this.elementAt(0)
.
Source
Future<T> get first { _Future<T> future = new _Future<T>(); StreamSubscription subscription; subscription = this.listen( (T value) { _cancelAndValue(subscription, future, value); }, onError: future._completeError, onDone: () { try { throw IterableElementError.noElement(); } catch (e, s) { _completeWithErrorCallback(future, e, s); } }, cancelOnError: true); return future; }