firebaseAuth.getCurrentUser() return null DisplayName

前端 未结 9 1891
[愿得一人]
[愿得一人] 2020-12-09 05:24

When I signIn with my google account and get the name with the getDisplayName(), my name appear correctly, but in the AuthStateListener doesn\'t.

here part of my cod

9条回答
  •  长情又很酷
    2020-12-09 05:45

    Just to add to Ymmanuel's answer (Thank you!) with some example code for anyone else looking for a quick copy and paste:

    FirebaseUser user = firebaseAuth.getCurrentUser();
    if (user != null) {
        // User is signed in              
        String displayName = user.getDisplayName();
        Uri profileUri = user.getPhotoUrl();
    
        // If the above were null, iterate the provider data
        // and set with the first non null data
        for (UserInfo userInfo : user.getProviderData()) {
            if (displayName == null && userInfo.getDisplayName() != null) {
                displayName = userInfo.getDisplayName();
            }
            if (profileUri == null && userInfo.getPhotoUrl() != null) {
                profileUri = userInfo.getPhotoUrl();
            }
        }
    
        accountNameTextView.setText(displayName);
        emailTextView.setText(user.getEmail());
        if (profileUri != null) {
            Glide.with(this)
                    .load(profileUri)
                    .fitCenter()
                    .into(userProfilePicture);
        }
    }
    

    The above will try to use the first display name and photo url from the providers if it wasn't initially found in the User object.

    Bonus: Using glide for images: https://github.com/bumptech/glide .

提交回复
热议问题