简体   繁体   中英

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

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

One way to pick up the current user in the second class is to use an auth state listener. 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).

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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