Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Could not find the correct Provider above the BlocListener Widget

I’m trying to use Bloc provider for user authentication in my flutter app. When I try to access the data i’m always getting this error even though I double checked all the files.

This is the error i’m getting:

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

This happens because you used a `BuildContext` that does not include the provider

main.dart:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

void main() async {
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(
            create: (context) => AuthBloc(LoginInitState(), AuthRepository()))
      ],
      child: MaterialApp(
        title: 'Flutter app',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity),
        home: const LoginPage(),
      ),
    );
  }
}

parts from login.dart:

@override
  void initState() {
    authBloc = BlocProvider.of<AuthBloc>(context);
    super.initState();
  }

######################################################

return Scaffold(
      backgroundColor: Colors.grey[300],
      body: BlocListener(
        listener: (context, state) {
          if (state is UserLoginSuccessState) {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => const HomeScreen()));
          }
        },
        child: SafeArea...

I’m still new to flutter and struggling with the state management part, I’d be glad if anybody can help!

>Solution :

In your BlocListener you’re missing the State and the Bloc

Here’s what I mean

BlocListener<AuthBloc, AuthState>(
        listener: (context, state) {
          if (state is UserLoginSuccessState) {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => const HomeScreen()));
          }
        },
        child: SafeArea...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading