【问题标题】:Updating dislayName in Firebase gives a syntax error在 Firebase 中更新 displayName 会出现语法错误
【发布时间】:2020-10-11 09:09:12
【问题描述】:

我正在尝试使用以下代码更新displayName。但我收到一条错误消息,说无法解析 displayName。看起来它没有将 {displayName:'test name', photoURL:'url to photo'} 作为参数。我正在关注 Firebase 示例和我在其他地方找到的示例,但无法使其正常工作。感谢您的帮助。

currentUser.updateProfile({displayName:"test name",photoURL:"url to photo"}); 

【问题讨论】:

    标签: java android firebase firebase-authentication


    【解决方案1】:

    FirebaseUser 的 updateProfile(UserProfileChangeRequest request) 方法将 UserProfileChangeRequest 类型的对象作为参数。因此,您无法将两个 String 对象传递给该方法。为了解决这个问题,您应该通过调用 UserProfileChangeRequest.Builder 的 setDisplayName(String displayName)setPhotoUri(Uri photoUri) 方法以及 build() 来构造该对象,该方法返回您正在寻找的确切对象。在代码中应该是这样的:

    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
        .setDisplayName("test name")
        .setPhotoUri("url to photo")
        .build();
    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    firebaseUser.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                Log.d("displayName: ", FirebaseAuth.getInstance().getCurrentUser().getDisplayName());
            }
        }
    });
    

    logcat 中的结果将是新名称:

    test name
    

    【讨论】:

      猜你喜欢
      • 2021-01-20
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2018-04-11
      • 1970-01-01
      相关资源
      最近更新 更多