在Firebase中更新displayName会出现语法错误。

huangapple go评论73阅读模式
英文:

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(&quot;test name&quot;)
    .setPhotoUri(&quot;url to photo&quot;)
    .build();
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
firebaseUser.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
    @Override
    public void onComplete(@NonNull Task&lt;Void&gt; task) {
        if (task.isSuccessful()) {
            Log.d(&quot;displayName: &quot;, FirebaseAuth.getInstance().getCurrentUser().getDisplayName());
        }
    }
});

The result in the logcat will the new name:

test name

huangapple
  • 本文由 发表于 2020年10月11日 17:09:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64302328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定