简体   繁体   中英

Error: Could not find the correct Provider above this widget

This screen is a Drawer screen which take the auth bloc in order to provide user the info and enable him of logout. I got this error although I am using the correct provider

The following ProviderNotFoundError was thrown building Pets4allDrawer(dirty):
I/flutter (32011): Error: Could not find the correct Provider<AuthService> above this Pets4allDrawer Widget
I/flutter (32011): To fix, please:
I/flutter (32011):   * Ensure the Provider<AuthService> is an ancestor to this Pets4allDrawer Widget
I/flutter (32011):   * Provide types to Provider<AuthService>
I/flutter (32011):   * Provide types to Consumer<AuthService>
I/flutter (32011):   * Provide types to Provider.of<AuthService>()
I/flutter (32011):   * Ensure the correct `context` is being used. 

I want to know the problem why using Provider.of(context) does not work, It can not be found while calling it.

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:pets4all/blocs/authBloc.dart';
import 'package:provider/provider.dart';

class Pets4allDrawer extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final AuthService authService = Provider.of<AuthService>(context);
    final user$ = authService.user.where((user) => user != null);
    return StreamBuilder<FirebaseUser>(
      stream: user$,
      builder: (context, snap) {
        final user = snap.data;
        if (snap.hasData) {
          return Drawer(
            child: ListView(
              children: <Widget>[
                ListTile(
                  leading: Icon(Icons.person_outline),
                  title: Text(user.displayName),
                  onTap: null,
                ),

                ListTile(
                  leading: Icon(Icons.home),
                  title: Text("Home"),
                  onTap: null,
                ),


                Align(
                  heightFactor: 3.5,
                  alignment: Alignment.bottomLeft,
                  child: FlatButton(
                    child: Text(
                      'Log out',
                      style: TextStyle(color: Colors.redAccent),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                      authService.signOut();
                    },
                  ),
                ),
              ],
            ),
          );
        } else {
          return CircularProgressIndicator();
        }
      },
    );
  }
}

and this is where I am calling the drawer

class TabScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    List<String> lol = ["questions", "events"];
    return StatefulProvider<ForumServices>(
        valueBuilder: (BuildContext context) => ForumServices(),
        child: Consumer<ForumServices>(
            builder: (BuildContext context, forumServices) {
          return StreamBuilder<List<String>>(
              stream: forumServices.forumsTypes$,
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return CircularProgressIndicator();
                }
                List<String> types = snapshot.data;
                num tabLen = types.length;

                return DefaultTabController(
                  length: tabLen,
                  child: Scaffold(
                    drawer: Pets4allDrawer(),

Check whether you have registered the provider in some ancestor Widget, as in the example:

 return MultiProvider(
    providers: [
      ChangeNotifierProvider(
        builder: (_) => FirebaseNotificationNotifier(),
      ),],

Update:

provider version provider: ^6.0.0

Declaration of provider in main myApp()

return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => LoginProvider()),
        ChangeNotifierProvider(create: (_) => SignupProvider()),
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          // This is the theme of your application.
          //
          // Try running your application with "flutter run". You'll see the
          // application has a blue toolbar. Then, without quitting the app, try
          // changing the primarySwatch below to Colors.green and then invoke
          // "hot reload" (press "r" in the console where you ran "flutter run",
          // or simply save your changes to "hot reload" in a Flutter IDE).
          // Notice that the counter didn't reset back to zero; the application
          // is not restarted.
          primarySwatch: Colors.blue,
        ),
        home: const MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );

if you using multiple providers, you should add those to main.dart.

return MultiProvider(
  providers: [
    ChangeNotifierProvider.value(
      value: Car(),
    ),
    ChangeNotifierProvider.value(
      value: Van(),
    ),
    ChangeNotifierProvider.value(
      value: Bus(),
    ),
  ],
  child: MaterialApp(
    title: 'MyApp',
    home: MyHome(),
  ),
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM