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


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: [
      create: (BuildContext context) {
        return DatabaseBloc();
      create: (BuildContext context) {
        return QuestionholderBloc();
                  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(
                    style: TextStyle(fontWeight: FontWeight.bold),
                  textColor: Colors.white70,
                  onPressed: () {

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

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

 Widget build(BuildContext context) {
return Scaffold(
  body: MultiBlocListener(
    listeners: [
      BlocListener<QuestionholderBloc, QuestionholderState>(
        listener: (context, state) {
          if (state is NoQuestions) {
          } else if (state is HasQuestions) {
      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: No ancestor could be found starting from the context that was passed to
I/flutter ( 4318): BlocProvider.of().


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.