BlocProvider.of() called with a context that does not contain a Cubit of type

756 views Asked by At

I'm sure there is something that I did wrong, but I cant seem to find what it is exactly.

I have the following register_screen.dart

class RegisterScreen extends StatelessWidget {
  static Route route() {
    return MaterialWithModalsPageRoute<void>(builder: (context) => RegisterScreen());
  }

  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;
    final colorScheme = Theme.of(context).colorScheme;

    return MultiBlocProvider(
      providers: [
        BlocProvider<ValidationCubit>(
          create: (BuildContext context) => ValidationCubit(context.repository<AuthenticationRepository>()),
        ),
        BlocProvider<RegisterBloc>(
          create: (BuildContext context) => RegisterBloc(),
        )
      ],
      child: MultiBlocListener(
        listeners: [
          BlocListener<ValidationCubit, ValidationCubitState>(
            listener: (context, state) {
              switch (state.status) {
                case FormzStatus.submissionSuccess:
                  Navigator.of(context).push(CodeSubmit.route());
                  break;

                default:
                  break;
              }
            },
          ),
          BlocListener<RegisterBloc, RegisterState>(
            listener: (context, state) {

            },
          ),
        ],
        child: PhoneRegister(),
      ),
    );

  }
}

When the form get's submited from PhoneRegister() a FormzStatus.submissionSuccess get's emitted. This is working perfectly. Now when I get navigated to CodeSubmit.route(), I can't seem to use the ValidationCubit

code_submit.dart

class CodeSubmit extends StatefulWidget {
  static Route route() {
    return MaterialPageRoute<void>(builder: (context) => CodeSubmit());
  }

  @override
  _CodeSubmitState createState() => _CodeSubmitState();
}

class _CodeSubmitState extends State<CodeSubmit> {
  @override
  Widget build(BuildContext context) {
    final ValidationCubit _signUpCubit = BlocProvider.of<ValidationCubit>(context);

    final textTheme = Theme.of(context).textTheme;
    final colorScheme = Theme.of(context).colorScheme;

    return Container();
  }
}

Why is BlocProvider.of(context) failing to fetch the ValidationCubit eventho it got passed using MultiBlocProvider?

Error I'm having:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building CodeSubmit(dirty, state: _CodeSubmitState#cc39e):
        BlocProvider.of() called with a context that does not contain a Cubit of type
ValidationCubit.
        No ancestor could be found starting from the context that was passed to
BlocProvider.of<ValidationCubit>().

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

        The context used was: CodeSubmit(dirty, state: _CodeSubmitState#cc39e)


The relevant error-causing widget was:
  CodeSubmit
  file:///home/lamba/Projects/Flutter/myapp/lib/screens/register/view/phone_register/code_submit.dart:15:58

When the exception was thrown, this was the stack:
#0      BlocProvider.of (package:flutter_bloc/src/bloc_provider.dart:113:7)
#1      _CodeSubmitState.build (package:myapp/screens/register/view/phone_register/code_submit.dart:25:55)
#2      StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15)
#4      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#5      Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#6      StatefulElement.update (package:flutter/src/widgets/framework.dart:4751:5)
#7      Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#8      SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14)
#9      Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#10     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#11     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#12     StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5)
#13     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#14     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14)
#15     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#16     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14)
#17     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#18     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#19     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#20     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#21     StatefulElement.update (package:flutter/src/widgets/framework.dart:4751:5)
#22     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#23     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14)
#24     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#25     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14)
#26     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#27     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#28     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#29     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#30     StatefulElement.update (package:flutter/src/widgets/framework.dart:4751:5)
#31     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#32     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#33     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#34     StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5)
#35     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#36     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#37     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#38     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#39     StatefulElement.update (package:flutter/src/widgets/framework.dart:4751:5)
#40     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#41     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14)
#42     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#43     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#44     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#45     ProxyElement.update (package:flutter/src/widgets/framework.dart:4906:5)
#46     _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:183:11)
#47     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#48     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14)
#49     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#50     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#51     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#52     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#53     StatefulElement.update (package:flutter/src/widgets/framework.dart:4751:5)
#54     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#55     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#56     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#57     ProxyElement.update (package:flutter/src/widgets/framework.dart:4906:5)
#58     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#59     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#60     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#61     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#62     StatefulElement.update (package:flutter/src/widgets/framework.dart:4751:5)
#63     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#64     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#65     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#66     StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5)
#67     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#68     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14)
#69     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#70     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#71     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#72     ProxyElement.update (package:flutter/src/widgets/framework.dart:4906:5)
#73     Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15)
#74     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16)
#75     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11)
#76     Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5)
#77     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2667:33)
#78     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:866:20)
#79     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:286:5)
#80     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15)
#81     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1056:9)
#82     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:865:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
1

There are 1 answers

0
Er1 On

You need to wrap your new page with a BlocProvider to be able to use the bloc on your new route.

Navigator.of(context).push(
  MaterialPageRoute(
    builder: (_) => BlocProvider.value(
      value: BlocProvider.of<ValidationCubit>(context),
      child: CodeSubmit(),
    ),
  ),
)