1

I need to create a new Stream<Foo> from EventChannel in my plugin, I try to do it like this, but i had Stream<Foo?>, what I mast to do for filtred all null values and return as 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
  }
EvGeniy Ilyin
  • 1,817
  • 1
  • 21
  • 38
  • 1
    [For `Iterable`s, the typical approach would be to use `.whereType`](https://stackoverflow.com/a/66896866/). Alas, [`Stream` does not have a `.whereType` method](https://github.com/dart-lang/sdk/issues/34050). See the linked GitHub issue for an example that combines `Stream.where()` and `Stream.cast()` instead. – jamesdlin Aug 18 '21 at 10:19

1 Answers1

0

You can use cast at the end like this:

final nonNullableStream = nullableStream.cast<Foo>();
Amir_P
  • 8,322
  • 5
  • 43
  • 92