Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:async
  3. Stream<T>
  4. single property
single
description

single property

Future<T> getsingle

The single element of this stream.

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

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

Implementation

Future<T> get single {  _Future<T> future = _Future<T>();  late T result;  bool foundResult = false;  StreamSubscription<T> subscription = this.listen(    null,    onError: future._completeError,    onDone: () {      if (foundResult) {        future._complete(result);        return;      }      var stack = StackTrace.empty;      var error = IterableElementError.noElement();      _trySetStackTrace(error, stack);      _completeWithErrorCallback(future, error, stack);    },    cancelOnError: true,  );  subscription.onData((T value) {    if (foundResult) {      // This is the second element we get.      var stack = StackTrace.empty;      var error = IterableElementError.tooMany();      _trySetStackTrace(error, stack);      _cancelAndErrorWithReplacement(subscription, future, error, stack);      return;    }    foundResult = true;    result = value;  });  return future;}
  1. Dart
  2. dart:async
  3. Stream<T>
  4. single property
Stream class

[8]ページ先頭

©2009-2025 Movatter.jp