Ramesh-X
Ramesh-X

Reputation: 5055

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?

Upvotes: 6

Views: 385

Answers (2)

keplerian
keplerian

Reputation: 562

Alternatively, you can use whereType<T>() in the stream_transform Dart dev package, like so:

import 'package:stream_transform/stream_transform.dart';

Stream<int> convertStream(Stream<String> s) {
  return s
    .map<int?>((i) => convert(i))
    .whereType<int>(); // <--
}

Upvotes: 1

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76243

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])

Upvotes: 7

Related Questions