0

Here is the main page:

child: BlocProvider(
    create: (_) => WelcomeCubit(context.read<AuthenticationRepository>()),
    child:
        WelcomeForm(),

And in the WelcomeForm

return BlocListener<WelcomeCubit, WelcomeState>(listener: (context, state) {
  print("BlocListener " + state.status.toString());                                                 
     if (state is newPage){
       Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => BlocProvider<WelcomeCubit>.value(
                  value: welcomeCubit,
                  child: SmsVerifyPage(),
                ))); }

On the SmsVerifyPage :

@override
Widget build(BuildContext context) {
SizeConfig().init(context);
return BlocBuilder<WelcomeCubit, WelcomeState>(
  buildWhen: (previous, current) => previous.otp != current.otp,
  builder: (context, state) {
    print(state.status.toString());
  >  print(state.phone.value); /////// <--------------- this value is always null.
    return Scaffold();}

I may be trying wrong at this point, I guess. but I couldn't find a solution to solve this. I referred all flutter bloc recipes but in vain I couldn't find one.

Thanks for any solution and suggestions.

EngineSense
  • 3,266
  • 8
  • 28
  • 44

1 Answers1

0

The BlocBuilder tries to find an instance of WelcomeCubit using the context provided. The context here comes from your Navigator which is an ancestor of your WelcomeForm so there is no WelcomeCubit available using that context. One solution to this problem is to wrap MaterialApp (which provides root navigator) with your BlocProvider.

Amir_P
  • 8,322
  • 5
  • 43
  • 92