Cerber
Cerber

Reputation: 73

Can't convert object of type java.lang.Boolean to type

I have a problem with an object namely when creating a list of objects it gets an error like in the title.

Kotlin code

 private fun readNotification() {
        val firebaseUser = FirebaseAuth.getInstance().currentUser
        val reference = FirebaseDatabase.getInstance().getReference("Notifications")
            .child(firebaseUser!!.uid)
        reference.addValueEventListener(object : ValueEventListener{
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                notificationList.clear()
                for (snapshot : DataSnapshot in dataSnapshot.children){
                    val notification = snapshot.getValue(NotificationData::class.java)!!
                    notificationList.add(notification)
                }
                notificationList.reverse()
                notificationAdapter.notifyDataSetChanged()
            }

            override fun onCancelled(error: DatabaseError) {
            }
        })
    }

Object code

class NotificationData(
    var userId: String = "",
    var text: String = "",
    var postId: String = "",
    var isPost: Boolean = false
) {}

Firebase structure

Firebase structure

I know the error is on this line because the object has a boolean variable

val notification = snapshot.getValue(NotificationData::class.java)!!

I just don't know much how to deal with it now

Upvotes: 0

Views: 512

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599661

While I thought the Firebase JSON mapper handled isBla format getters, there is only one boolean in your JSON, so it makes sense that the problem comes form there.

You could change the boolean to this:

var post: Boolean = false

Or add an explicit @PropertyName annotation:

@PropertyName("post")
var isPost: Boolean = false

That said, I think it's a valid expectation for the Firebase mapper to handle boolean isXyz properties by default, so would recommend adding a feature request on the Github repo of the SDK.

Upvotes: 1

Related Questions