英文:
Updating dislayName in Firebase gives a syntax error
问题
我正在尝试使用以下代码更新 displayName
。但是我收到一个错误,提示无法解析 displayName
。看起来它不接受 {displayName:'test name', photoURL:'url to photo'}
作为参数。我正在遵循 Firebase 的示例以及我在其他地方找到的示例,但无法使其工作。感谢您的帮助。
currentUser.updateProfile({displayName:"test name", photoURL:"url to photo"});
英文:
I am trying to update the displayName
with the following code. But I get an error saying cannot resolve displayName. Looks like it doesn't take {displayName:'test name', photoURL:'url to photo'} as a parameter. I am following the Firebase example and examples I found elsewhere, but can't get it to work. Thank you for your help.
currentUser.updateProfile({displayName:"test name",photoURL:"url to photo"});
答案1
得分: 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(Uri.parse("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
英文:
FirebaseUser's updateProfile(UserProfileChangeRequest request) method, takes as an argument an object of type UserProfileChangeRequest. So there is no way you can pass two String objects to that method. To solve this, you should construct that object by calling UserProfileChangeRequest.Builder's setDisplayName(String displayName) and setPhotoUri(Uri photoUri) methods along with build(), a method that returns the exact object that you are looking for. In code it should look like this:
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());
}
}
});
The result in the logcat will the new name:
test name
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论