简体   繁体   English

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

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

I have a working google sign in and sign out learn from the tutorial 我有一个工作正常的Google登录并注销,从本教程中学习

but I don't know how to access it from another class. 但我不知道如何从另一个班级访问它。 I want user profile picture from the login screen to home screen. 我希望用户个人资料图片从登录屏幕转到主屏幕。

_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);
                      });
                    });
                  });

this is my code for sign in I want to access signedInUser.displayName from another class as well as signedInUser.photourl 这是我的登录代码,我想从另一个类以及signedInUser.photourl访问signedInUser.displayName

One way to pick up the current user in the second class is to use an auth state listener. 在第二类中选择当前用户的一种方法是使用auth状态侦听器。 The simplest way is: 最简单的方法是:

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

This listen callback will fire whenever the authentication state changes, and you can use it to read properties from the user (or to update the UI to reflect the authentication state). 每当身份验证状态更改时,都会触发此侦听回调,您可以使用它从用户读取属性(或更新UI以反映身份验证状态)。

You can also ensure authentication in the second class (replicating part of what you now do already in the first class), or pass the data using shared preferences. 您还可以确保在第二堂课中进行身份验证(复制您现在在第一堂课中所做的部分工作),或使用共享首选项传递数据。 For examples of all three approaches, see Firebase Login with Flutter using onAuthStateChanged . 有关这三种方法的示例,请参阅使用onAuthStateChanged使用Flutter登录Firebase

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

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