Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get currently logged in user email in side-nav-bar in flutter?

I want to get the email of currently logged in user in accountEmail field. But it is displaying Instance of 'Future' in place of email. Below is the function to get the id of currently logged in user:

Future <String> getUserId()  async{
  FirebaseUser user = await FirebaseAuth.instance.currentUser();
  return user.uid;
}

And in this way I am getting it:

new UserAccountsDrawerHeader(
                accountName: new Text("Admin"),
                accountEmail: new Text(getUserId().toString()),
              ),

I am new to flutter and any help would be highly appreciated.

like image 983
STBox Avatar asked Dec 19 '25 07:12

STBox


2 Answers

Inside the initState, do the following:

FirebaseUser user = null;
@override
void initState() {
    super.initState();
    user = await FirebaseAuth.instance.currentUser();
}

Then you can do:

new UserAccountsDrawerHeader(
                accountName: new Text("Admin"),
                accountEmail: new Text(user.email.toString()),
              ),
like image 153
Peter Haddad Avatar answered Dec 21 '25 00:12

Peter Haddad


Try using FutureBuilder

 UserAccountsDrawerHeader(
      accountName: new Text("Admin"),
      accountEmail: FutureBuilder(
        future: getUserId(),
        builder: (context,snapshot){
          if(snapshot.hasData){
            snapshot.data;
          }else{
            "-"
          }
        },
      ),
    ),
like image 23
Sikshya Maharjan Avatar answered Dec 21 '25 02:12

Sikshya Maharjan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!