Search code examples
flutterbloc

Why flutter bloc says, "Bad state: Cannot emit new states after calling close." While I ma passing the bloc from prvious screen?


I have created a bloc on 1 bloc on home page.

That is passed to drawer then profile page and profile update page.

During the whole flow I just close the drawer ad navigate to next page to when users close back. Hhe does have to see already open drawer but.

Scaffold.of(context).closeEndDrawer();

But in profile update page.

emit(AppBaseState.busy());

Isn't working due to following error:

Bad state: Cannot emit new states after calling close.

I am expectiing to udate profile normally.


Solution

  • You might check the doc:

    In some cases, BlocProvider can be used to provide an existing bloc to a new portion of the widget tree. This will be most commonly used when an existing bloc needs to be made available to a new route. In this case, BlocProvider will not automatically close the bloc since it did not create it.

    BlocProvider.value(
      value: BlocProvider.of<BlocA>(context),
      child: ScreenA(),
    );
    

    then from either ChildA, or ScreenA we can retrieve BlocA with:

    // with extensions
    context.read<BlocA>();
    
    // without extensions
    BlocProvider.of<BlocA>(context)