How do I make a method that returns a stream<T> after modifying another stream<U>

Viewed 32

I would like to make a helpfer method which takes a stream from a library (draw in this case) and changes the stream from Stream<UserContent> to Stream<Submission> and also filters the Submissions for duds.

So far I have this code but how do I return the stream now?

  Stream<Submission> getSavedPosts({int limit = 20}) async* {
    yield await _me!.saved(limit: limit).map((UserContent userContent) async {
      try {
        Submission sub = await _populateUserContent(userContent);
        if (_filterPost(sub)) {
          return sub;
        }
      } catch (err) {
        print(err);
      }
    }); // The type 'Stream<Stream<Future<Submission?>>>' implied by the 'yield' expression must be assignable to 'Stream<Submission>'.
  }
1 Answers

you should use asyncMap method on the stream rather than map.

Stream<ClassA> getOldStream() {}

Stream<ClassA> getNewStream() {
  return getOldStream().asyncMap((event) async {
    return event;
  });
}

Note: yield and async* should be used in a generator function; mapping doesn't need a generator function

Related