Error: Could not find the correct Provider above this widget

Aya Elsisy picture Aya Elsisy · May 1, 2019 · Viewed 14.9k times · Source

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(),

Answer

Sachin picture Sachin · Aug 21, 2019

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

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