Retrieving user email from Firebase in Flutter

1,449

Solution 1

Since your getCurrentUserEmail returns a Future, you'll need to use a FutureBuilder to use it in your build method.

accountEmail: FutureBuilder<String>(
  future: AuthProvider.of(context).auth.getCurrentUserEmail(),
  builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
    if (snapshot.hasData) {
      return Text(snapshot.data)
    }
    else {
      return Text("Loading user data...")
    }

  }
)

Solution 2

The best thing to do is to upgrade to firebase_auth:0.18.0, after upgrade you can get the currentUser synchronously!

dependencies:
  flutter:
    sdk: flutter
  firebase_core : ^0.5.0
  firebase_auth : ^0.18.0

initialize Firebase:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

Then in UsersAccountDrawerHeader:

   UserAccountsDrawerHeader(
      accountName: Text('Brad Pitt'),
      accountEmail: Text('${auth.instance.currentUser.email}'),

Also check:

Undefined class 'FirebaseUser'

No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp() in Flutter and Firebase

Solution 3

Retrieving user email, null safety supported.

var currentUser = FirebaseAuth.instance.currentUser;


Text('admin email: ${FirebaseAuth.instance.currentUser!.email}'),

Solution 4

You need to add ~await~ in front of the function as it's a function that returns a ~Future~

await AuthProvider.of(context).auth.getCurrentUserEmail()
Share:
1,449
hermie_brown
Author by

hermie_brown

Flutter Enthusiast! All things mobile and cool.

Updated on December 23, 2022

Comments

  • hermie_brown
    hermie_brown over 1 year

    In my AuthProvider class where I handle my sign in, sign, out authentications, I created 2 functions that returns a Future String like so

      Future<String> currentUser() async {
        FirebaseUser user = await _auth.currentUser();
        return user.uid;
      }
    
    
      Future<String> getCurrentUserEmail() async {
        FirebaseUser user = await _auth.currentUser();
        final String email = user.email.toString();
      //  print(email);
        return email;
      }
    

    In my menu screen, I want to display my current signed in user email in a text field and I am calling it as below.

        UserAccountsDrawerHeader(
          accountName: Text('Brad Pitt'),
          accountEmail: Text(
              '${AuthProvider.of(context).auth.getCurrentUserEmail()}'),
    

    I have tried using both the currenUser() and getCurrentUserEmail() to try to display the loggedIn user's email but I keep getting a "Instance of Future" displayed.

    Is there something I'm overlooking here? I've tried every possible hack I can think of.

    Thanks.

  • hermie_brown
    hermie_brown almost 4 years
    I can't call this in my build method. it would throw an error and force me to declare my default build function as a Future and adds async to it.
  • hermie_brown
    hermie_brown almost 4 years
    Thanks. Current dependencies version are firebase_core: ^0.4.5 firebase_auth: ^0.16.1 cloud_firestore: ^0.13.7 Taking on your suggestion, what version of Cloud Firestore would be compatible with the above updates to firebase_auth and firebase_core?
  • Peter Haddad
    Peter Haddad almost 4 years
    0.14.0 and core 0.5.0 as in the answer, auth 0.18.0
  • Peter Haddad
    Peter Haddad almost 4 years
  • hermie_brown
    hermie_brown almost 4 years
    This doesn't solve my issue either. I'm getting red and yellow scribbly lines. The error message reads: A build function returned bull.The offending widget is: FutureBuilder<String> Build functions must never return null. To return an empty space that causes the building widget to fill available room, return "Container()". To return an empty space that takes as little room as possible, return "Container(width: 0.0, height: 0.0)".
  • Frank van Puffelen
    Frank van Puffelen almost 4 years
    Ah, that might have been due to missing returns statements, so I updated my answer to add those. I also added a link to the documentation about FutureBuilder. I'd highly recommend you learn about that (what it is, why it's needed here, how it works), so you can troubleshoot follow-up issues yourself.
  • hermie_brown
    hermie_brown almost 4 years
    Thanks! I appreciate. Seems I have to make app-wide changes with these new updates.