1

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

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Abhishek Razy
  • 1,151
  • 7
  • 8
  • You can either ensure the user is signed in from the second class too, attach an auth state listener, or pass the data to the second class with for example shared preferences. For examples of all three, see https://stackoverflow.com/questions/45353730/firebase-login-with-flutter-using-onauthstatechanged – Frank van Puffelen Nov 24 '18 at 01:47

1 Answers1

0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807