свойство single
Future<T> singleВозвращает единственный элемент.
Если событие ошибки происходит до или после первого события данных, результирующее будущее завершается с этой ошибкой.
Если this пусто или содержит более одного элемента, выбрасывается StateError.
Исходный код
Future<T> get single {
_Future<T> future = new _Future<T>();
T result = null;
bool foundResult = false;
StreamSubscription subscription;
subscription = this.listen(
(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;
},
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.dartlang.org/stable/1.24.3/dart-async/Stream/single.html