简体   繁体   中英

How to get logged in user's email in Flutter using Firebase

I want to get the email of the logged in user in a Flutter app which uses Firebase for authentication.

I can get the current user by

final user = await _auth.currentUser();

But if I try this to get the mail

final mailID = await _auth.currentUser().email.toString();

I get the following error:

The getter 'email' isn't defined for the type 'Future<FirebaseUser>'.
Try importing the library that defines 'email', correcting the name to the name of an existing getter, or defining a getter or field named 'email'.

How to get the logged in user's email in this case?

Get the user before trying to get the email. code below

<FirebaseUser> user = await _auth.currentUser();
final mailID = user.email;

it's working for me.

import 'package:firebase_auth/firebase_auth.dart';


FirebaseAuth auth = FirebaseAuth.instance;


FirebaseAuth.instance
    .authStateChanges()
    .listen((User? user) {
  if (user == null) {
    print('User is currently signed out!');
  } else {
    print('User is signed in!');
  }
 });

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