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.

hermie_brown
  • 319
  • 9
  • 24

5 Answers5

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

  }
)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 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 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)".** – hermie_brown Aug 21 '20 at 16:24
  • 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. – Frank van Puffelen Aug 21 '20 at 16:29
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

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • 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? – hermie_brown Aug 21 '20 at 16:03
  • 0.14.0 and core 0.5.0 as in the answer, auth 0.18.0 – Peter Haddad Aug 21 '20 at 16:04
  • 1
    Check https://stackoverflow.com/questions/63473865/the-getter-instance-isnt-defined-for-the-type-firestore/63473889#63473889 – Peter Haddad Aug 21 '20 at 16:05
  • Thanks! I appreciate. Seems I have to make app-wide changes with these new updates. – hermie_brown Aug 21 '20 at 16:45
2

Retrieving user email, null safety supported.

var currentUser = FirebaseAuth.instance.currentUser;


Text('admin email: ${FirebaseAuth.instance.currentUser!.email}'),
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()
ByteMe
  • 1,575
  • 1
  • 12
  • 23
  • 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 Aug 21 '20 at 15:40
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}',),
Kanu mike
  • 1
  • 3