Flutter returns "No ancestor could be found starting from the context that was passed to I/flutter"

Issue

I cannot figure out what is wrong with this code. There is a bloc provider on this page:

Widget build(BuildContext context) {
return MultiBlocProvider(
  providers: [
    BlocProvider(
      create: (BuildContext context) {
        return DatabaseBloc();
      },
    ),
    BlocProvider(
      create: (BuildContext context) {
        return QuestionholderBloc();
      },
    )
  ],`child:RaisedButton(
                  splashColor: Colors.green[600],
                  focusElevation: 50.0,
                  hoverElevation: 50.0,
                  color: Colors.green[600],
                  shape: RoundedRectangleBorder(
                      side: BorderSide(color: Colors.yellow[700]),
                      borderRadius: BorderRadius.circular(50.0)),
                  child: Text(
                    "Start",
                    style: TextStyle(fontWeight: FontWeight.bold),
                  ),
                  textColor: Colors.white70,
                  onPressed: () {
                    ExtendedNavigator.of(context)
                        .pushNamed(Routes.questionHandler);
                  }),)

sorry for the possible syntax error.
In the QuestionHolderPage I have:

 void didChangeDependencies() {
_statusbloc = BlocProvider.of<QuestionholderBloc>(context);
_dbbloc = BlocProvider.of<DatabaseBloc>(context);
_statusbloc.add(CheckStatus());
super.didChangeDependencies();
}

 Widget build(BuildContext context) {
return Scaffold(
  body: MultiBlocListener(
    listeners: [
      BlocListener<QuestionholderBloc, QuestionholderState>(
        listener: (context, state) {
          if (state is NoQuestions) {
            _dbbloc.add(GetQuestions());
          } else if (state is HasQuestions) {
            _statusbloc.add(GetQuestion());
          }
        },
      ),
      BlocListener<DatabaseBloc, DatabaseState>(
        listener: (context, state) {
          if (state is Loaded) {
            _statusbloc.add(SetQuestions(questionsToSet: state.questions));
          }
        },
      ),
    ],
    child: BlocBuilder<QuestionholderBloc, QuestionholderState>(
      builder: (context, state) {
        if (state is QuestionLoaded) {
          return QuestionWidget(question: state.question);
        } else {
          return CircularProgressIndicator();
        }
      },
    ),
  ),
);
}
}

Error:

Error: No ancestor could be found starting from the context that was passed to
I/flutter ( 4318): BlocProvider.of().

Solution

As can be seen from your code, you are navigating from where you initialise your MultiBlocProvider because of that you are getting this error.

You can not access provider if you initialise in one screen and access it after navigating to other screen.

First way to solve.

If you want to do, so can initialise your MultiBlocProvider above MaterialApp widget, so you can access in each and every screen of your apllication.

Second way:

You can pass all the value which you want to access in other screen when you are navigating and initialise new MultiBlocProvider in that screen.

Answered By – Viren V Varasadiya

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

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