Marko
Marko

Reputation: 5397

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

Upvotes: 3

Views: 12704

Answers (3)

user14792809
user14792809

Reputation:

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

Upvotes: 2

Morad Al-Rowaihi
Morad Al-Rowaihi

Reputation: 76

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

Upvotes: 0

Bostrot
Bostrot

Reputation: 6033

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

Upvotes: 5

Related Questions