简体   繁体   中英

How to get current user email address FirebaseAuth/Flutter

I want to get the email of the currently signed in user, but I can't seem to find a way to do it. I presume it should be something similar to:

FirebaseAuth.instance.currentUser().getEmail(); //The .getEmail() part is made up

Assuming you are using the Firebase Auth package:

final FirebaseAuth _auth = FirebaseAuth.instance; 

Future<FirebaseUser> _handleSignIn() async { 
GoogleSignInAccount googleUser = await _googleSignIn.signIn(); 
GoogleSignInAuthentication googleAuth = await googleUser.authentication; 
FirebaseUser user = await _auth.signInWithGoogle( 
accessToken: googleAuth.accessToken,
 idToken: googleAuth.idToken, ); 

// get email here
print("signed in " + user.email); 
return user; }

And handling the login:

_handleSignIn() .then((FirebaseUser user) => print(user)) .catchError((e) => print(e));

In order to get the signed-in user information, you should first call the currentUser() method, and then call for the email and save it into a string variable, like this:

    final _auth = FirebaseAuth.instance;
    String userEmail;
    void getCurrentUserEmail() async {
    final user =
              await _auth.currentUser().then((value) => userEmail = value.email);
    }

Another way of doing this is by saving currentUser() into a variable then fetching for related information such as email or phoneNumber by typing . and choosing any value you want , like this:

    final _auth = FirebaseAuth.instance;
    dynamic user;
    String userEmail;
    String userPhoneNumber;

    void getCurrentUserInfo() async {
      user = await _auth.currentUser();
      userEmail = user.email;
      userPhoneNumber = user.phoneNumber;
      }

It is very easy to do... you just have to get the current user with FirebaseAuth.instance.currentUser and get the email by adding .email

You will get the email of the current user

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