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

Flutter Bloc Listen to stream and emit state with Cubit

How can I listen to stream and emit state with Cubit?

With Bloc we can do something like this:

    on<VideoStreamPlayPauseEvent>(
      (event, emit) async {
        if (event.play) {
          await emit.forEach(
            videoStreamingRepo.videoDataStream,
            onData: (VideoData videoStreamData) => VideoStreamState(
              currentFrame: videoStreamData,
              isPlaying: true,
            ),
          );
        }

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 :

You should listen to the stream using listen()
and based on the event you can emit the desired state.

yourstream.listen((event) {
      if (event.play)
          emit(First State)
      else
          emit(Second State)
    });

Extra: Based on the demand of questioner

To Stop listening to stream:
StreamSubscription<Map<PlaceParam, dynamic>> subscription;
subscription = yourstream.listen((event) {
      if (event.play)
          emit(First State)
      else
          emit(Second State)

      ...
   
      subscription.cancel(); 👈 cancel this way
    });

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