英文:
When reading from Firestore I get a deserialize error
问题
我正在尝试读取Firestore中的一个集合。为此,我想将相应的文档转换为我的POJO类。但是,我得到了下面列出的错误。
我的数据类模型:
data class NewObjektPojo(
val objektHauptBild: String? = "",
val objektName: String? = "",
val objektBeschreibung: String? = "",
val objektBilder: MutableList<String>? = null,
val objektZimmer: Number? = 0,
val objektGröße: Number? = 0,
val objektPreis: Number? = 0,
val objektLatitude: Double? = null,
val objektLongitude: Double? = null
)
引发错误的部分:
db.collectionGroup("Houses").get().addOnSuccessListener { snapshot ->
for (document in snapshot.documents) {
// 错误发生在这里
val house = document.toObject(NewObjektPojo::class.java)
objektListe.add(house!!)
}
}
java.lang.RuntimeException: 无法反序列化对象。不支持将值反序列化为Number(在字段'objektGröße'中找到)
类型应该匹配:
英文:
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:
答案1
得分: 0
你之所以收到此错误是因为 Number
不是一种数据类型。请使用 Long
数据类型来定义这些变量
将这些:
val objektZimmer: Number? = 0,
val objektGröße: Number? = 0,
val objektPreis: Number? = 0,
替换为:
val objektZimmer: Long? = 0L,
val objektGröße: Long? = 0L,
val objektPreis: Long? = 0L,
英文:
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,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论