如何获取当前用户的电子邮件地址FirebaseAuth / Flutter

时间:2018-07-05 12:53:17

标签: firebase dart firebase-authentication flutter

我想获取当前已登录用户的电子邮件,但似乎找不到解决方法。 我认为它应该类似于:

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

3 个答案:

答案 0 :(得分:3)

假设您使用的是Firebase Auth软件包:

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

处理登录信息:

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

答案 1 :(得分:0)

为了获取登录的用户信息,您应该首先调用currentUser()方法,然后调用email并将其保存到字符串变量中,如下所示:

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

另一种方法是将currentUser()保存到变量中,然后通过键入email并选择所需的任何值来获取相关信息,例如phoneNumber. , 像这样:

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

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

答案 2 :(得分:0)

这很容易做到……您只需使用 FirebaseAuth.instance.currentUser 获取当前用户并通过添加 .email

获取电子邮件

您将收到当前用户的电子邮件