Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase won't bind boolean value to field

I'm using Google's firebase-database SDK for Android, v9.0.1. I have my app hooked up to Firebase and can read and write data at various locations.

However, I cannot get a specific boolean field to bind using dataSnapshot.getValue(PingReport.class) and I keep getting an error in my logs that says No setter/field for isUp found on class com.myapp.PingReport when the field clearly exists in my model.

Here's the JSON in the Firebase database:

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

and here's the model 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;
    }
}

If I call getDurationMs() or getTimestampMillis() the correct values are returned, but the value returned from isUp() is always false. I have tried different combinations of naming it up and isUp and mUp and adding setters setUp(boolean up) and setIsUp(boolean isUp), but nothing seems to work. The documentation for the Android SDK not very detailed. Is there some trick or detail I'm overlooking?

like image 596
Jschools Avatar asked Jun 03 '16 22:06

Jschools


3 Answers

I ran into this problem in Kotlin. I had several boolean values. All of them were set properly except for the one that started with is. To fix this, I made it a custom getter and that fixed the problem:

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
    }
}
like image 109
Peter Graham Avatar answered Nov 09 '22 17:11

Peter Graham


If your boolean field is named isUp, then the getter must be named isIsUp() or getIsUp(). Alternatively, if you want a getter named isUp, the field name would be up.

like image 25
Bob Snyder Avatar answered Nov 09 '22 18:11

Bob Snyder


Alternatively, you can use the @PropertyName annotation from Firebase Database to handle this. Also, it's better to include setters as well.

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;
}

}

like image 4
Kamal Kishore Avatar answered Nov 09 '22 17:11

Kamal Kishore