16

此屏幕是一个抽屉屏幕,它采用 auth bloc 以向用户提供信息并使他能够注销。尽管我使用了正确的提供程序,但我收到了这个错误

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) 不起作用,调用它时找不到它的问题。

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

这就是我打电话给抽屉的地方

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

2 回答 2

10

检查您是否在某个祖先 Widget 中注册了提供程序,如示例中所示:

 return MultiProvider(
    providers: [
      ChangeNotifierProvider(
        builder: (_) => FirebaseNotificationNotifier(),
      ),],
于 2019-08-21T09:37:31.380 回答
0

如果你使用多个提供者,你应该将它们添加到 main.dart。

return MultiProvider(
  providers: [
    ChangeNotifierProvider.value(
      value: Car(),
    ),
    ChangeNotifierProvider.value(
      value: Van(),
    ),
    ChangeNotifierProvider.value(
      value: Bus(),
    ),
  ],
  child: MaterialApp(
    title: 'MyApp',
    home: MyHome(),
  ),
);
于 2021-06-26T06:00:42.317 回答