Jus*_*s10 5 future stream dart
我想等一个bool是真的,然后从Future返回,但我似乎无法让我的Future等待Stream.
Future<bool> ready() {
return new Future<bool>(() {
StreamSubscription readySub;
_readyStream.listen((aBool) {
if (aBool) {
return true;
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*ams 11
您可以使用Stream方法firstWhere
创建在Stream发出true
值时解析的未来.
Future<bool> whenTrue(Stream<bool> source) {
return source.firstWhere((bool item) => item);
}
Run Code Online (Sandbox Code Playgroud)
没有stream方法的替代实现可以使用await for
Stream上的语法.
Future<bool> whenTrue(Stream<bool> source) async {
await for (bool value in source) {
if (value) {
return value;
}
}
// stream exited without a true value, maybe return an exception.
}
Run Code Online (Sandbox Code Playgroud)
Future<void> _myFuture() async {
Completer<void> _complete = Completer();
Stream.value('value').listen((event) {}).onDone(() {
_complete.complete();
});
return _complete.future;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2420 次 |
最近记录: |