无法通过StreamProvider获取Firebase用户

时间:2020-06-16 00:17:20

标签: firebase flutter flutter-provider

我在第一个窗口小部件中注册了Firebase用户,并在第二个屏幕中登录后尝试访问它。我得到的结果为空,并且出现以下错误:Could not find the correct provider above this widget.经过研究,我仍然找不到原因。我猜测上下文可能不在某个地方,但是仍然不知道为什么。

这是我的第一个小部件:

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Chat App',
            debugShowCheckedModeBanner: false,
            theme: ThemeData(
                primarySwatch: Colors.blueGrey,
                visualDensity: VisualDensity.adaptivePlatformDensity,
                inputDecorationTheme: InputDecorationTheme(
                    border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(20),
                )),
                buttonTheme: ButtonThemeData(buttonColor: Colors.blueGrey)),
            home: MultiProvider(providers: [
              StreamProvider<FirebaseUser>.value(
                value: FirebaseAuth.instance.onAuthStateChanged,
              )
            ], child: SignInPage()));
      }
    }

这是我第二个屏幕的摘录:

     RaisedButton(
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(40)),
                    child: ListTile(
                      leading: Container(
                        color: Colors.white,
                        child: Image.network(
                          'https://cdn4.iconfinder.com/data/icons/new-google-logo-2015/400/new-google-favicon-512.png',
                          width: 25,
                          height: 25,
                        ),
                      ),
                      title: Text(
                        'Sign In with Google',
                        style: TextStyle(
                            color: Colors.white, fontWeight: FontWeight.bold),
                      ),
                    ),
                    onPressed: () async {
                      setState(() {
                        _progressBarActive = true;
                      });
                      FirebaseUser user =
                          Provider.of<FirebaseUser>(context, listen: false);
                      await signInWithGoogle().then((value) {
                        print(value.uid);
    //                    print(user.uid);
                        Navigator.pushReplacement(context,
                            MaterialPageRoute(builder: (context) => ChatRoom()));
                      }).whenComplete(() => {
                            setState(() {
                              _progressBarActive = false;
                            })
                          });
                    },
                  ),

1 个答案:

答案 0 :(得分:0)

MultiProviderMaterialApp交换:

return MultiProvider(
      providers: [
        StreamProvider<FirebaseUser>.value(
          value: FirebaseAuth.instance.onAuthStateChanged,
        )
      ],
      child: MaterialApp(
        home: SignInPage(),
      ),
    );