firebaseAuth.getCurrentUser() return null DisplayName

前端 未结 9 1869
[愿得一人]
[愿得一人] 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:48

    Based on Alan's and Ymmanuel's answers here's the 2 helper methods that I'm using:

    public static String getDisplayName(FirebaseUser user) {
        String displayName = user.getDisplayName();
        if (!TextUtils.isEmpty(displayName)) {
            return displayName;
        }
    
        for (UserInfo userInfo : user.getProviderData()) {
            if (!TextUtils.isEmpty(userInfo.getDisplayName())) {
                return userInfo.getDisplayName();
            }
        }
    
        return null;
    }
    
    public static Uri getPhotoUrl(FirebaseUser user) {
        Uri photoUrl = user.getPhotoUrl();
        if (photoUrl != null) {
            return photoUrl;
        }
    
        for (UserInfo userInfo : user.getProviderData()) {
            if (userInfo.getPhotoUrl() != null) {
                return userInfo.getPhotoUrl();
            }
        }
    
        return null;
    }
    

提交回复
热议问题