skip to Main Content

Please help

This is my MyApp Class

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
  debugShowCheckedModeBanner: false,
  title: 'Instagram',
  theme: ThemeData(
    primarySwatch: Colors.blue,
    inputDecorationTheme: InputDecorationTheme(
      enabledBorder: OutlineInputBorder(
        borderSide:
            BorderSide(width: 1, color: Colors.white.withOpacity(0.2)),
      ),
      focusedBorder: OutlineInputBorder(
        borderSide:
            BorderSide(width: 1, color: Colors.white.withOpacity(0.2)),
      ),
    ),
  ),

  // here I am providing the cubit to my SplashCubit Class

  home: BlocProvider(
      create: (context) => SplashCubit()..checkSavedDetails(),
      child: const SplashScreen()),
);}}

this is my SplashScreen class

class SplashScreen extends StatelessWidget {
const SplashScreen({super.key});

@override
Widget build(BuildContext context) {
return BlocBuilder<SplashCubit,SplashState>(
    builder: (context, state) {
  return AnimatedSplashScreen(
      duration: 3000,
      centered: true,
      splash: Image.asset('assets/images/instagram_logo.jpg'),
      backgroundColor: Colors.black,
      nextScreen: state.dataSaved
          ? HomePage()
          : LoginPage(),
});}}

please help, Not able to figure out what is getting wrong

I checked everything but it is giving the below error.

enter image description here

2

Answers


  1. You need to setup up MultiBlocProvider with all the Providers for your Cubits first and then make MaterialApp (and SplashScreen) a child of MultiBlocProvider. It should look something like this:

    class MyApp extends StatelessWidget {
    const MyApp({super.key});
    
    @override
    Widget build(BuildContext context) {
      
      
    // Add your provider for the cubit here using MultiBlocProvider
    return MultiBlocProvider(
      providers: [
        BlocProvider<SplashCubit>(
          create: (context) => SplashCubit(),
          ),
      ],
      // MaterialApp (and SplashScreen) need to be a child of MultiBlocProvider
      child: MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Instagram',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        inputDecorationTheme: InputDecorationTheme(
          enabledBorder: OutlineInputBorder(
            borderSide:
                BorderSide(width: 1, color: Colors.white.withOpacity(0.2)),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide:
                BorderSide(width: 1, color: Colors.white.withOpacity(0.2)),
          ),
        ),
      ),
    
      
    
      home: SplashScreen()),
    );
    
    }}
    
    Login or Signup to reply.
  2. If you are using SplashBloc only for Splash Screen.Then you can try this

    class SplashScreen extends StatelessWidget {
      const SplashScreen({super.key});
    
      @override
      Widget build(BuildContext context) {
        return BlocProvider(                   <-----------  add bloc provider here
          create: (context) => SplashCubit(),
          child:BlocBuilder<SplashCubit,SplashState>(
              builder: (context, state) {
                return AnimatedSplashScreen(
                    duration: 3000,
                    centered: true,
                    splash: Image.asset('assets/images/instagram_logo.jpg'),
                backgroundColor: Colors.black,
                nextScreen: state.dataSaved
                ? HomePage()
                : LoginPage(),
              });,
        );}}}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search