【发布时间】:2021-08-18 10:10:56
【问题描述】:
我需要在我的插件中从 EventChannel 创建一个新的Stream<Foo>,我尝试这样做,但我有Stream<Foo?>,我要做的就是过滤所有空值并返回为Stream<Foo>
Stream<Foo> get getStream {
return _eventChannel
.receiveBroadcastStream()
.map<Foo?>((dynamic status) {
assert(
status is int,
'status should be int, but it is ${status.runtimeType}',
);
if (status is int) {
return FooStatus.valueOf(status); // can be null
}
}).helpMeFiltredLikeThis<Foo>((Foo? value) => value != null); // how i can do some like this
}
【问题讨论】:
-
For
Iterables, the typical approach would be to use.whereType。唉,Streamdoes not have a.whereTypemethod。请参阅链接的 GitHub 问题,了解结合Stream.where()和Stream.cast()的示例。