Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Firebase Deserialization

I'm trying to deserialize data from Firebase to POJO with Kotlin, this is my POJO class:

class Message {
var number: String ?= null
var message: String? = null
var timestamp: Long = 0L
var isHandled: Boolean ?= false
var type:String ?=null
}

and this is my code to retrieve data from Firebase

val query = Fire.REF.child("sms").orderByChild("type").equalTo("outbox-unsent")
    query.addChildEventListener(object : ChildEventListener {
        override fun onChildAdded(dataSnapshot: DataSnapshot, key: String) {
            val outbox = dataSnapshot.getValue(Message::class.java)
            Log.d("BS", "Background Service $outbox")
           }
//......
});

this is my data structure on Firebase look like

"sms":{
"-KI3ar91oBXGNpXXrOCS" : {
"handled" : false,
"message" : "Pak kenapa koneksi...",
"number" : "+6285830166314",
"timestamp" : 1463587399000,
"type" : "inbox"
},
//.......//
}

When I execute the code it raises an error:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter key

Where is my mistake on above code?

like image 793
Putra Ardiansyah Avatar asked Oct 12 '25 08:10

Putra Ardiansyah


1 Answers

As Augusto said above, Kotlin is null-safe, but on above function, the key parameter may null. So the code should like this

onChildAdded(dataSnapshot: DataSnapshot, key: String?)

Add question mark after key parameter

like image 134
Putra Ardiansyah Avatar answered Oct 14 '25 22:10

Putra Ardiansyah