3

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.

5 Answers 5

4

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...")
    }

  }
)
Sign up to request clarification or add additional context in comments.

2 Comments

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)".
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.
3

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

4 Comments

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?
0.14.0 and core 0.5.0 as in the answer, auth 0.18.0
Thanks! I appreciate. Seems I have to make app-wide changes with these new updates.
2

Retrieving user email, null safety supported.

var currentUser = FirebaseAuth.instance.currentUser;


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

Comments

1

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

1 Comment

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.
0

After initilizing your Firebase in your main

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

Now in the Text widget to display the current user's email use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.