错误:在此小部件上方找不到正确的提供者 [英] Error: Could not find the correct Provider above this widget

查看:68
本文介绍了错误:在此小部件上方找不到正确的提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此屏幕是抽屉"屏幕,该屏幕使用auth bloc以便向用户提供信息并使他能够注销.尽管我使用了正确的提供程序

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. 

我想知道为什么无法使用Provider.of(context),在调用它时找不到它的问题.

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

这篇关于错误:在此小部件上方找不到正确的提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆