0

I'm trying to implements a Cubit that is used to make the navigation bewteen the pages. My first try was to implement it on my background screens, but I got this error when trying to do it :

"Error: Could not find the correct Provider<StateStreamable<Object?>> above this BlocBuilder<StateStreamable<Object?>, Object?> Widget"

I don't understand why it doesn't find the correct context because my BlocBuilder is above the BlocProvider in the widget tree ...

Here's the code :

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flexmes_mobile_app/buisness_logic/cubit/internet_cubit.dart';
import 'package:flexmes_mobile_app/buisness_logic/utility/app_bloc_observer.dart';
import 'package:flexmes_mobile_app/config/themes.dart';
import 'package:flexmes_mobile_app/ui/screens/auth_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:responsive_sizer/responsive_sizer.dart';
import 'buisness_logic/cubit/navigation_cubit.dart';
import 'config/app_router.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Firebase.initializeApp();
  BlocOverrides.runZoned(
        () => runApp(MyApp()),
    blocObserver: AppBlocObserver(),
  );
}

class MyApp extends StatelessWidget {
  final AppRouter _appRouter = AppRouter();

  MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MediaQuery(
      data: const MediaQueryData(),
      child: ResponsiveSizer(
          builder: (context, orientation, deviceType) {
            return MultiBlocProvider(
              providers: [
                BlocProvider<InternetCubit>(
                  create: (context) => InternetCubit(connectivity: Connectivity()),
                ),
                BlocProvider<NavigationCubit>(
                  create: (context) => NavigationCubit(),
                ),
              ],
              child: MaterialApp(
                title: 'Flexmes Mobile App',
                //Generate routes for navigation
                onGenerateRoute: _appRouter.generateRoute,
                //Take the correct theme to apply to the screens
                theme: appThemeData[AppTheme.authTheme],
                //Build the default widget, the "background" widget
                builder: (context, child) {
                  //Instantiate the Cubits
                  BlocProvider.of<InternetCubit>(context);
                  BlocProvider.of<NavigationCubit>(context);
                  return BlocBuilder(
                    builder: (BuildContext context, state) {
                      if (state is AuthenticationInitial){
                        return AuthScreen(child: child);
                      }
                      return Container();
                    },

                  );
                },
              ),
            );
          }
      ),
    );
  }
}

Does anyone knows why ? :)

Thanks for your answers !

Chris

h8moss
  • 4,626
  • 2
  • 9
  • 25
Zouglou
  • 89
  • 1
  • 11

1 Answers1

3
  • what worked for is writing the type of Cubit and State in the BlocBuilder too so from that :

return BlocBuilder( builder: (BuildContext context, state) {

                  if (state is AuthenticationInitial)

                                            {

                    return AuthScreen(child: child);

                  }

                  return Container();

                },

it will be :

return BlocBuilder<BlocCubit,BlocState>(

          builder: (BuildContext context, state) {

                  if (state is AuthenticationInitial)

                                            {

                    return AuthScreen(child: child);

                  }

                  return Container();

                },

that is what works for me

Youssef
  • 96
  • 7