Bloc: onError Bad state: Cannot add new events after calling close

Created on 19 Sep 2019  路  3Comments  路  Source: felangel/bloc

Hi ,
I faced this error when visit page again
steps: open page -> back -> open page again

I read other guys asked about this error, but in my code I didn't dispose any bloc because I use BlocProvider in the routes config

routes: {
        HomepagePage.routeName: (BuildContext context) =>
            BlocProvider<HomepageBloc>(
              builder: (context) => HomepageBloc(),
              child: HomepagePage(),
            ),
        OnboardingScreen.routeName: (BuildContext context) =>
            OnboardingScreen(),
        ProductDetailPage.routeName: (BuildContext context) =>
            BlocProvider<ProductDetailBloc>(
              builder: (context) => ProductDetailBloc(),
              child: ProductDetailPage(),
            ),
        ProductListPage.routeName: (BuildContext context) =>
            BlocProvider<ProductListBloc>(
              builder: (context) => ProductListBloc(),
              child: ProductListPage(),
            ),
        LoginPage.routeName: (BuildContext context) => 
            BlocProvider<LoginBloc>(
              builder: (context) => LoginBloc(),
              child: LoginPage(),
            )
}

And in your release blog I found that it is automatically disposed.

As you can see, we no longer have to worry about disposing our blocs! As long as they are created by BlocProvider they will be disposed by BlocProvider automatically 馃帀.

But if it is automatically disposed, how can I deal with above error. Thanks

Most helpful comment

yes, I already reported bug to him. thanks

All 3 comments

just found the reason:

This is because I am using a Singleton bloc for the screen. So it is not getting initialized once the screen is going out of the Widget tree.

The bloc template from https://github.com/Gorniv/vscode-flutter-files uses singleton bloc.
I think it need to be updated.

@nguyenhuutinh thanks for the update. I would recommend opening an issue on that repo as it is maintained by a third party. You can check out the official vscode extension if you haven't already 馃憤

yes, I already reported bug to him. thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clicksocial picture clicksocial  路  3Comments

abinvp picture abinvp  路  3Comments

hivesey picture hivesey  路  3Comments

komapeb picture komapeb  路  3Comments

1AlexFix1 picture 1AlexFix1  路  3Comments