DocumentReference.set(Object o) 更改布尔字段名称

Mah*_*Gvp 6 android firebase google-cloud-firestore

使用时DocumentReference.set(object),布尔字段会发生变化。带有“is”前缀的字段更改为普通字段值

class Invitation {
    private boolean isRequested;
    private boolean isValid;
    private boolean isAccepted;
    private String lastName,firstName;
    private long sentOn;
}
Run Code Online (Sandbox Code Playgroud)

当我使用set()方法将此对象推送到数据库时,布尔值以这种方式更改:

Firestore 截图

Gri*_*orr 5

这是一个自动翻译,因此可以命名 getterisBoolean而不是getBoolean.

在 Android 中,您可以使用@PropertyName注释重命名属性,这将允许您指定不同的名称(在本例中为确切名称),Firebase 应按原样使用该名称:

class Invitation {
    private boolean isRequested;

    @PropertyName(value="isRequested")
    public boolean isRequested() {
        return this.isRequested;
    }

    @PropertyName(value="isRequested")
    public void setRequested(boolean value) {
        this.isRequested = value;
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是,我建议删除is字段名称上的前缀,而仅将其用于 getter,例如:

public boolean isRequested() {
    return this.requested;
}
Run Code Online (Sandbox Code Playgroud)