我想等待布尔真真,然后从future 归来,但我似乎不能让我的future 等待溪流.
Future<bool> ready() {
return new Future<bool>(() {
StreamSubscription readySub;
_readyStream.listen((aBool) {
if (aBool) {
return true;
}
});
});
}
我想等待布尔真真,然后从future 归来,但我似乎不能让我的future 等待溪流.
Future<bool> ready() {
return new Future<bool>(() {
StreamSubscription readySub;
_readyStream.listen((aBool) {
if (aBool) {
return true;
}
});
});
}
您可以使用Stream方法firstWhere
来创建在Stream发出true
值时解析的future .
Future<bool> whenTrue(Stream<bool> source) {
return source.firstWhere((bool item) => item);
}
没有流方法的替代实现可以在流上使用await for
语法.
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.
}