繁体   English   中英

Google Flutter Firebase登录以及如何从多个类访问

[英]flutter firebase google sign in and how to access from multiple classes

我有一个工作正常的Google登录并注销,从本教程中学习

但我不知道如何从另一个班级访问它。 我希望用户个人资料图片从登录屏幕转到主屏幕。

_googleSignIn.signIn().then((result) {
                    result.authentication.then((googleKey) {
                      FirebaseAuth.instance
                          .signInWithGoogle(
                              idToken: googleKey.idToken,
                              accessToken: googleKey.accessToken)
                          .then((signedInUser) {                         
                        print(
                            'Signed in as ${signedInUser.displayName} ${signedInUser.photoUrl}');
                        widget.onSignIn();
                      }).catchError((e) {
                        print(e);
                      }).catchError((e) {
                        print(e);
                      }).catchError((e) {
                        print(e);
                      });
                    });
                  });

这是我的登录代码,我想从另一个类以及signedInUser.photourl访问signedInUser.displayName

在第二类中选择当前用户的一种方法是使用auth状态侦听器。 最简单的方法是:

FirebaseAuth.instance.onAuthStateChanged.listen((user) {
  print(user);
});

每当身份验证状态更改时,都会触发此侦听回调,您可以使用它从用户读取属性(或更新UI以反映身份验证状态)。

您还可以确保在第二堂课中进行身份验证(复制您现在在第一堂课中所做的部分工作),或使用共享首选项传递数据。 有关这三种方法的示例,请参阅使用onAuthStateChanged使用Flutter登录Firebase

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM