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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With