Home > Mobile >  When reading from Firestore I get a deserialize error
When reading from Firestore I get a deserialize error

Time:01-09

I'm trying to read a collection in Firestore. For this I want to cast the respective document into my POJO class. However, I get the error listed below

My data class model:

data class NewObjektPojo(
val objektHauptBild: String? = "",
val objektName: String? = "",
val objektBeschreibung: String? = "",
val objektBilder: MutableList<String>? = null,
val objektZimmer: Number? = 0,
val objektGroeße: Number? = 0,
val objektPreis: Number? = 0,
val objektLatitude: Double? = null,
val objektLontitude: Double? = null,
)

The part which throws the error:

 db.collectionGroup("Houses").get().addOnSuccessListener { snapshot ->
        for(document in snapshot.documents)
        {
            //mistake happens here
            val house = document.toObject(NewObjektPojo::class.java)
            objektListe.add(house!!)
        }
 }

java.lang.RuntimeException: Could not deserialize object. Deserializing values to Number is not supported (found in field 'objektGroeße')

The type should fit:

enter image description here

CodePudding user response:

You are getting this error because Number is not a datatype. Use Long datatype for these variables

Replace these

val objektZimmer: Number? = 0,
val objektGroeße: Number? = 0,
val objektPreis: Number? = 0,

With These

val objektZimmer: Long? = 0L,
val objektGroeße: Long? = 0L,
val objektPreis: Long? = 0L,
  • Related