Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert a stream to another type without using map?

I’ve this code:

Stream<int> get fooStream async* {
  barStream.listen((_) async* { 
    int baz = await getBaz();
    yield baz; // Does not work
  });  
}

How can I return Stream<int> from another stream?


Note: If I use map to transform the stream, then I’ll have to return Stream<Future<int>>, but I want to return Stream<int>. I also don’t feel like using rxdart pacakge for this tiny thing.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  1. Use asyncMap.

    barStream.asyncMap((e) => getBaz())
    
  2. Use await for

    Stream<int> get fooStream async* {
     await for (final item in barStream) {
       yield await getBaz();
     }
    }
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading