Search code examples
dartoption-type

How to convert Stream<T?> to Stream<T>


I have the following function that will return int? by accepting String.

int? convert(String s) {
  // Some implementation
}

Then I have this function that will convert a Stream<String> to Stream<int>. My current implementation looks like the following.

Stream<int> convertStream(Stream<String> s) {
  return s
    .map<int?>((i) => convert(i))
    .where((i) => i!=null)
    .map<int>((i) => i!);
}

This way doesn't look nice to me. Is there any other way that can be used to achieve the same behavior?


Solution

  • Your code is correct.

    Unlike Iterable, Stream doesn't have a whereType method. So you can't do s.map(convert).whereType<int>().

    You can replace .map<int>((i) => i!) by .cast<int>().

    Alternativelly you can use an async* method:

    Stream<int> convertStream(Stream<String> input) async* {
      await for (var s in input) {
        var i = convert(s);
        if (i != null) {
          yield i;
        }
      }
    }
    

    EDIT: As suggested by @lrn you can also use expand:

    stream.expand<int>((int? v) => v == null ? [] : [v])