0

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().

1
  • You seem to have a backtick character in the first block of code (`), it looks like it is out of place. If that is just a type here, and not in your code, please repair it anyway.
    – halfer
    May 16, 2020 at 11:18

1 Answer 1

2

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.

3
  • Quick question: Does the BuildContext change while passing through routes? May 16, 2020 at 13:19
  • Yes definitely! May 16, 2020 at 13:22
  • Thanks for the info. Appreciate It. May 16, 2020 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.