How can Bloc listen to stream and emit state
Asked Answered
H

2

17

In my flutter app, I use flutter_bloc for state management.

The bloc in question uses a repository. The repository subscribes to a websocket, and new data is added to a stream.

Problem: My bloc listens to the stream:

InvestmentClubBloc({
    required this.investmentClubRepository
  }) : super(InvestmentClubLoading()) {
    onChangeSubscription = investmentClubRepository.onNewMessage.listen(
      (event) {
        emit(NewMessageState(event); // <-- The member "emit" can only be used within 'package:bloc/src/bloc.dart' or in test
      },
    );
  }

The problem is that emit does not work (I get the warning "The member "emit" can only be used within 'package:bloc/src/bloc.dart' or in test")

How can bloc listen to a stream and emit new states depending on stream events?

Harrelson answered 6/12, 2021 at 10:12 Comment(4)
Which flutter_bloc version do you use?Uneven
flutter_bloc: ^8.0.0Harrelson
Please check my answer below.Uneven
Thank you for the nice questionSpiccato
M
31

You should use emit in eventHandler, use below code to complete your task:

abstract class Event {}

class DemoEvent extends Event {}

var fakeStream =
    Stream<int>.periodic(const Duration(seconds: 1), (x) => x).take(15);

class DemoBloc extends Bloc<Event, int> {
  DemoBloc() : super(0) {
    fakeStream.listen((_) {
      // add your event here
      add(DemoEvent());
    });
    on<DemoEvent>((_, emit) {
      // emit new state
      emit(state + 1);
    });
  }
}
Microfiche answered 6/12, 2021 at 10:53 Comment(5)
To clarify: Even though we listen on the stream in the bloc, the bloc should add events to itself? I do recognise this solution would work, but it seems like a lot of overhead code.Harrelson
The code is based on your use case. If you have a stream, maybe a StreamBuilder can fulfill your requirement though, you don't need bloc then.Uneven
hey, maybe you could help me out here? #72716531Hagood
this really given the logic to overcome the issue last night, but hey in the listening part you definetely use a StreamBuilder, this is how I achieved the work. Even I have another bloc builder also, so the logic was, I returned the BlocBuilder inside StreamBuilder builder property, and applied my stream logic between if(snapshot.hasData) {... ...}Separate
Don't forget cancel subscription in closeWaldman
O
11

you should use emit.forEach( )
where forEach must return a state which will be emitted

like this

await emit.forEach(yourStream, onData: (dynamic coming_data) {
  return your_state;
}).catchError((error) {
  emit error;
});
Oxide answered 31/3, 2022 at 23:39 Comment(1)
hey maybe you could help me out here? #72716531Hagood

© 2022 - 2024 — McMap. All rights reserved.