Bloc: can't dispatch event with error: Bad state: Cannot add new events after calling close

Created on 26 Sep 2019  路  10Comments  路  Source: felangel/bloc

I dispatch a new event but got this error when "_eventSubject.sink.add(event)" called in dispatch() method in bloc.dart file

question

All 10 comments

Hey bro! I just resolve this problem by adding * await _eventSubject.drain();
await _stateSubject.drain();
* in dispose() method in bloc.dart file. I'm not sure that is good practice. But at that time, i only found this solution

Hi @nguyenhieu283 馃憢
Thanks for opening an issue!

This means that you are dispatching an event after dispose has been called. Can you please share the link to a sample app which illustrates the problem you're seeing?

Adding drain would prevent the error but it is masking the actual issue.

Firstly, I dispatch a LoginEventViaGoogle() event to AuthBloc and yield AuthenticatedState after login success and immediately change Login screen to User screen. It work perfectly. But after that i want to dispatch LogoutEvent() to return User screen and get an above error. Also, i don't call bloc.dispose anywhere in my app.

Can you share the link to the application or a sample which illustrates the problem? Blocs are disposed automatically by BlocProvider so the problem might have to do with where in the tree the bloc is being provided.

I really want to share the link for you. But i can't because this project is company private. Sorryyy.
I only describe you about this. I have a home widget where init _authBloc. In this Home widget i provide this _authBloc to child widget (AccountScreen widget) . An in AccountScreen i get _authBloc via Provider.of<>. command. After that i dispatch LoginEventViaGoogle() and yield AuthenticatedState when login success. In Home widget I place BlocBuilder widget to listener state change when authenticated. It work ok. But when i click Sign out and dispatch LogoutEvent(), it return this error. I completely code same as your samples.

Can you build a super simplified version of your application that illustrates the problem you're having? If you're using the same code as the samples can you reproduce it in the samples? It's really hard to help without seeing the code unfortunately.

Here is my sample. Can you see it? https://github.com/nguyenhieu283/test-app

Yup I can see it but it doesn't compile. Either way I have noticed several things you should address:

  1. You're instantiating Blocs directly inside of StatelessWidgets which you should not do.

Instead of:

class MyHomePage extends StatelessWidget {
  final AuthenticationBloc _authenticationBloc = AuthenticationBloc(userRepository: UserRepository());
  final HomeBloc homeBloc = HomeBloc(dataRepository: DataRepository());

  @override
  Widget build(BuildContext context) {...}
}

You should instantiate your blocs inside of the builder method of BlocProvider like:

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<AuthenticationBloc>(
          builder: (context) => AuthenticationBloc(userRepository: UserRepository()),
        ),
        BlocProvider<HomeBloc>(
          builder: (context) => HomeBloc(dataRepository: DataRepository()),
        ),
      ],
      child: ...
    );
  }
}
  1. On lines 28 and 36 of home.dart you are using BlocProvider with a builder to and returning an existing bloc which is most likely the root cause of your problem. When you use BlocProvider with the builder you should always be building the bloc instead of providing an existing bloc. This is because using BlocProvider with builder automatically disposes of the bloc you're providing when the BlocProvider is disposed.

Hope that helps 馃憤

Thank you!! But it not working as expected

No problem! Have you made sure that you are only creating blocs inside of BlocProvider's builder? Also, please make sure you're not returning existing bloc instances within BlocProvider's builder (it should always only return a new bloc instance).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craiglabenz picture craiglabenz  路  3Comments

timtraversy picture timtraversy  路  3Comments

ricktotec picture ricktotec  路  3Comments

MahdiPishguy picture MahdiPishguy  路  3Comments

clicksocial picture clicksocial  路  3Comments