Home > other >  Flutter - I want to put the name and email of the user logged in the drawer
Flutter - I want to put the name and email of the user logged in the drawer

Time:11-20

i'm new to flutter and i managed to make the login screen, now i want to display the name and email in the drawer, can someone help me? follow my authentication code and firestore

this is my code to firestore:

Future<void> userSetup(String displayName, DateTime valitycnh, String url) async {
  CollectionReference users = FirebaseFirestore.instance.collection('Users_Client');
  FirebaseAuth auth = FirebaseAuth.instance;
  String uid = auth.currentUser!.uid.toString();
  users.add(
    {
      'uid': uid,
      'displayname': displayName,      
      'valitycnh': valitycnh,
      'url': url
    },
  );
  return;
}

here is my code aunthentication:

class AuthenticationHelper {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  get user => _auth.currentUser;

  //SIGN UP METHOD
  Future signUp(
      {required String email,
      required String password,
      required String username,
      required DateTime valitycnh,
      required String url}) async {
    try {
      await _auth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );
      User? updateUser = FirebaseAuth.instance.currentUser;
      updateUser!.updateDisplayName(username);
      userSetup(username, valitycnh, url);

      return null;
    } on FirebaseAuthException catch (e) {
      return e.message;
    }
  }


and i want a name and email here:

Drawer: Drawer(
        child: ListView(
          children: [
            UserAccountsDrawerHeader(
              decoration: BoxDecoration(
                color: AppColors.secundary,
              ),
              accountEmail: Text('email'),
              accountName: Text('name'),
            )
          ],
        ),
      ),

CodePudding user response:

You can use the getters available in FirebaseUser.

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

String name = user.getDisplayName();
String email = user.getEmail();

CodePudding user response:

You can use the FirebaseAuth instance to get user info:

Future<Map<String,dynamic>> _getUserDetails(){
       FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    
       if(user != null){
       return {
        'name' : user.getDisplayName(),
         'email' : user.getEmail();
         }
        }
      
    
     return {
      'name' : 'No name',
     'email' : 'No email'
    }

}
  • Related