19

I have my kotlin class as

class Center : Serializable {
var active: Boolean? = null
var address: String? = null
var isJobAccessGranted: Boolean? = null
}

here is how i am getting value

 //from java class
   @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            centerMap.put(dataSnapshot.getKey(), dataSnapshot.getValue(Center.class));
        }

but problem is that i am getting value of active field without any issue. But isJobAccessGranted boolean field always remains null. I have tested with some other boolean removing is prefix which works fine. I don't get Boolean value when i use isActive or isJobAccessGranted. Can anyone explain me why i am facing this issue. #AskFirebase

3
  • 1
    you have a property called isJobAccessGranted, so appropriate generated getter will be isIsJobAccessGranted. Maybe it is the issue?
    – Mike
    Commented Sep 25, 2017 at 13:42
  • no @mike it is not generating isIsJobAccessGranted Commented Sep 25, 2017 at 13:55
  • 8
    Dont know the exact reason behind this but using @field:JvmField over var isJobAccessGranted: Boolean? = null solved my problem Commented Sep 25, 2017 at 15:22

1 Answer 1

31

As mentioned in comment in Kotlin you should add @field:JvmField for every Boolean field in class, as when Kotlin translates to Java it generates setters, which names couldnt be resolved by JSON parser. And @field:JvmField annotation overrides this feature and there will be correct setters names and JSON will be parsed correctly.

3
  • 9
    Why is this not on the front page of the Firebase Kotlin docs ?
    – r3dm4n
    Commented Feb 7, 2019 at 6:55
  • 4
    @r3dm4n this is now on the Firestore docs. Thanks for the suggestion! Commented Nov 1, 2020 at 20:22
  • Sure yes, adding the @field:JvmField resolves it.
    – bensalcie
    Commented Dec 31, 2022 at 22:08

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.