【问题标题】:Flutter: How to filtrate data from `Stream<Foo?>` to `Stream<Foo>`Flutter:如何将数据从 `Stream<Foo?>` 过滤到 `Stream<Foo>`
【发布时间】:2021-08-18 10:10:56
【问题描述】:

我需要在我的插件中从 EventChannel 创建一个新的Stream&lt;Foo&gt;,我尝试这样做,但我有Stream&lt;Foo?&gt;,我要做的就是过滤所有空值并返回为Stream&lt;Foo&gt;

  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
  }

【问题讨论】:

标签: flutter plugins stream


【解决方案1】:

你可以像这样在末尾使用cast

final nonNullableStream = nullableStream.cast<Foo>();

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2018-11-05
    • 2019-08-30
    • 2023-04-01
    • 1970-01-01
    • 2020-06-07
    相关资源
    最近更新 更多