1

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?

Eagle
  • 318
  • 4
  • 16

2 Answers2

2

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

<FirebaseUser> user = await _auth.currentUser();
final mailID = user.email;
JideGuru
  • 7,102
  • 6
  • 26
  • 48
  • Thank you, but any explanation as to why this happens? I mean why not in one command? – Eagle Jun 19 '20 at 01:59
  • 1
    Ohh yea. You have to `await` `_auth.currentUser()` but if you write it in one line you're just trying to get the email from a `Future` – JideGuru Jun 19 '20 at 02:19
0

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!');
  }
 });