Error: Could not find the correct Provider above this widget
Asked Answered
C

3

17

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(),
Clotho answered 1/5, 2019 at 10:49 Comment(4)
I don't see you adding a AuthService anywhere. Is that normal?Drusilla
Probably when you're creating a new Pets4allDrawer you forgot to wrap it into Provider widget.Sirup
I solved the issue by wrapping the material app with a provider its value is the auth services this was the mistakeClotho
Did you get the answer, I am also stuckBartholomeus
V
11

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

 return MultiProvider(
    providers: [
      ChangeNotifierProvider(
        builder: (_) => FirebaseNotificationNotifier(),
      ),],
Vowell answered 21/8, 2019 at 9:37 Comment(0)
M
1

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'),
      ),
    );
Mimi answered 7/3, 2022 at 16:40 Comment(0)
S
0

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(),
  ),
);
Spastic answered 26/6, 2021 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.