Flutter Bloc : There is no ancestor of my bloc in the context founded while I have it well provided with a BlocProvider.value

Issue

Here is where I create my Bloc (main file):

if (state is AuthenticationAuthenticated) {
  return BlocProvider<NavigateHomeScreenBloc>(
      create: (context) => NavigateHomeScreenBloc(state.user!.uid)
        ..add(NavigateToProjects()),
      child: Home());
}

Here is where I display the page with the floating button (Home page):

if (state is NavigateHomeScreenDemandesScreen)
  return Demandes(state.demandesPageModel);

The bottom sheet of Scaffold from Demandes page :

bottomSheet: BlocProvider.value(
  value: BlocProvider.of<NavigateHomeScreenBloc>(context),
  child: AddCollaboratorButton(),
),

My floating button that pushes my page where the error of ancestor occurred (my BlocProvider.value is here -> AddCollaboratorButton class):

FloatingActionButton(
  onPressed: () {
    Navigator.of(context)
      .push(MaterialPageRoute(
        builder: (context) => BlocProvider.value(
          value:
          BlocProvider.of<NavigateHomeScreenBloc>(context),
          child: DemandeCollaboration())))
      .then((value) =>
            BlocProvider.of<NavigateHomeScreenBloc>(context)
            .add(NavigateToDemandes()));
  },
  child: Icon(Icons.add, color: Colors.white),
),

What I am trying to do on the _DemandeCollaborationState that creates my error (DemandeCollaboration page):

DatabaseService databaseService = DatabaseService(
        uid: BlocProvider.of<NavigateHomeScreenBloc>(context).uid);

Here is the error when I click on the floating button :

Screen shot error

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building Builder(dirty):
        BlocProvider.of() called with a context that does not contain a NavigateHomeScreenBloc.

        No ancestor could be found starting from the context that was passed to BlocProvider.of<NavigateHomeScreenBloc>().

        This can happen if the context you used comes from a widget above the BlocProvider.

        The context used was: Builder(dirty)

The relevant error-causing widget was
MaterialApp
lib/main.dart:65
When the exception was thrown, this was the stack
#0      BlocProvider.of
package:flutter_bloc/src/bloc_provider.dart:103
#1      _AddCollaboratorButtonState.build.<anonymous closure>.<anonymous closure>
package:app_apporteur_affaires/…/widgets/addCollaboratorButton.dart:34
#2      MaterialPageRoute.buildContent
package:flutter/…/material/page.dart:54
#3      MaterialRouteTransitionMixin.buildPage
package:flutter/…/material/page.dart:107
#4      _ModalScopeState.build.<anonymous closure>.<anonymous closure>
package:flutter/…/widgets/routes.dart:840
...
════════════════════════════════════════════════════════════════════════════════

Solution

Ok I’ve finally found the solution, I’ve define my bloc before the Navigator.of(context).push like this :

var myNagigateBloc = BlocProvider.of<NavigateHomeScreenBloc>(context);

And I’ve replaced the value of my BlocProvider.value by my myNagigateBloc like this :

onPressed: () {
  Navigator.of(context)
      .push(MaterialPageRoute(
          builder: (context) => BlocProvider.value(
              value: myNagigateBloc,
              child: DemandeCollaboration())))
      .then((value) =>
          BlocProvider.of<NavigateHomeScreenBloc>(context)
              .add(NavigateToDemandes()));
},

I think the problem was that my BlocProvider.value was inside a new context, from the build of the MaterialPageRoute.

Answered By – Dev Lulu

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *