Firebase 不会将布尔值绑定到字段

Firebase won't bind boolean value to field

我正在为 Android 使用 Google 的 firebase-database SDK,v9.0.1。我将我的应用连接到 Firebase,可以在不同位置读取和写入数据。

但是,我无法使用 dataSnapshot.getValue(PingReport.class) 获取要绑定的特定布尔字段,并且当我的模型中明显存在该字段时,我的日志中不断收到错误提示 No setter/field for isUp found on class com.myapp.PingReport

这是 Firebase 数据库中的 JSON:

{
    "durationMs": 364,
    "isUp": true,
    "timestampMillis": 1464916019971
}

这是模型 class:

public class PingReport {

    private long durationMs;
    private boolean isUp;
    private long timestampMillis;

    public PingReport() {
        // required by Firebase
    }

    public PingReport(long durationMs, boolean isUp, long timestampMillis) {
        this.durationMs = durationMs;
        this.isUp = isUp;
        this.timestampMillis = timestampMillis;
    }

    public long getDurationMs() {
        return durationMs;
    }

    public boolean isUp() {
        return isUp;
    }

    public long getTimestampMillis() {
        return timestampMillis;
    }
}

如果我调用 getDurationMs()getTimestampMillis() 会返回正确的值,但从 isUp() 返回的值始终是 false。我尝试了将其命名为 upisUp 以及 mUp 并添加设置器 setUp(boolean up)setIsUp(boolean isUp) 的不同组合,但似乎没有任何效果。 Android SDK 的文档不是很详细。我是否忽略了一些技巧或细节?

您 getter 的 isUp() 不符合正常惯例。你试过了吗public boolean getIsUp()

如果您的布尔字段命名为 isUp,则 getter 必须命名为 isIsUp()getIsUp()。或者,如果您想要一个名为 isUp 的 getter,字段名称将是 up.

您应该检查 java 中的命名约定。

尝试使用:

 private boolean up;
 public boolean isUp() {
        return up;
 }

不要使用原始 "boolean" 类型,而是使用 "Boolean" class。

或者,您可以使用 Firebase 数据库中的 @PropertyName 注释来处理此问题。另外,最好也包括设置器。

Pass a custom Java object, if the class that defines it has a default constructor that takes no arguments and has public getters for the properties to be assigned.

public class PingReport {

private long durationMs;
private boolean isUp;
private long timestampMillis;

public PingReport() {
    // required by Firebase
}

public PingReport(long durationMs, boolean isUp, long timestampMillis) {
    this.durationMs = durationMs;
    this.isUp = isUp;
    this.timestampMillis = timestampMillis;
}

public long getDurationMs() {
    return durationMs;
}

@PropertyName("isUp")
public boolean isUp() {
    return isUp;
}

public long getTimestampMillis() {
    return timestampMillis;
}

}

我 运行 在 Kotlin 中解决了这个问题。我有几个布尔值。除了以 is 开头的那个以外,其他都设置正确。为了解决这个问题,我将其设为自定义 getter 并解决了问题:

data class FirebaseEvent(
    val description: String? = null,
    val disableLogin: Boolean? = null,
    val isDVR: Boolean? = null
) {
    fun getIsDVR(): Boolean? {
        // this is needed to trick Kotlin into using this getter instead of generating its own which breaks firebase
        return isDVR
    }
}

不要将 "is" 与您的变量名一起使用,尤其是在 Firebase 实时数据库中。我也有同样的问题,我必须将我的变量名从 "isUserExist" 更改为 "userExist" !蹩脚的 Firebase!

如果您使用的是 Kotlin,则在变量的 getter 和 setter 上使用 PropertyName,这样您就可以绕过 kotlin 生成的 getters/setters 名称. 在变量本身上使用此注释没有帮助,因为它不会影响生成的 setters 和 setters.

data class(
   @get:PropertyName("isRead")
   @set:PropertyName("isRead")
   var isRead: Boolean? = null
)

您可以查看 了解有关 java

的更多信息