Firebase Getdisplayname() Returns Empty
Solution 1:
In order to get your display name , you will need to set it up when you create the new account with email and password
For example
mAuth.createUserWithEmailAndPassword(email,pass).addOnCompleteListener(newOnCompleteListener<AuthResult>() {
@OverridepublicvoidonComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
saveUser(email,pass,name);
FirebaseUseruser= mAuth.getCurrentUser();
UserProfileChangeRequestprofileUpdates=newUserProfileChangeRequest.Builder().setDisplayName(name).build();
user.updateProfile(profileUpdates);
finish();
...
Now, you will need an AuthStateListener
, and then when it's complete (you successful logged in or created the account), you can get the name. Since firebase manages this Asynchronous
mAuthListener = newFirebaseAuth.AuthStateListener() {
@OverridepublicvoidonAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUseruser= firebaseAuth.getCurrentUser();
if (user != null) {
usuario.setNome(firebaseUser.getDisplayName());
} else {
finish();
...
Check UserProfileChangeRequest Here
Important
If you log in with a provider like Google sign-in, it will handle automatically your display name (as well as the profile photo), and just calling user.getDisplayName()
without setting it will do the job correctly
Remember that when you create a new account with emailAndPassword, the ID of the account and the email are automatically stored at Firebase, that's why you can access those data without setting them. This is an example of the metadata that is created along with the user. In this case, the metadata defining your user is only the UserID and the Email.
But there is never set a displayName or a photoUri for that account, that's why you need to also set them up when you create a new account as I mentioned above.
Tip
Avoid doing your owns methods like this, you will be confused when the app scales.
publicstatic FirebaseAuth getFirebaseAutenticacao(){
if (auth == null) {
auth = FirebaseAuth.getInstance();
}
return auth;
}
instead, just use this
FirebaseAuthusuario= FirebaseAuth.getInstance();
And then use your usuario to get what you need
usuario.getCurrentUser().getUid(); //for example, getting the uid of the user logged in
Post a Comment for "Firebase Getdisplayname() Returns Empty"