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

954

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.

Share:
954
Abhishek Razy
Author by

Abhishek Razy

Updated on December 07, 2022

Comments

  • Abhishek Razy
    Abhishek Razy over 1 year

    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
      Frank van Puffelen over 5 years
      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 stackoverflow.com/questions/45353730/…