从Firestore读取时,我收到了反序列化错误。

huangapple go评论57阅读模式
英文:

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'中找到)

类型应该匹配:

从Firestore读取时,我收到了反序列化错误。

英文:

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? = &quot;&quot;,
val objektName: String? = &quot;&quot;,
val objektBeschreibung: String? = &quot;&quot;,
val objektBilder: MutableList&lt;String&gt;? = null,
val objektZimmer: Number? = 0,
val objektGroe&#223;e: Number? = 0,
val objektPreis: Number? = 0,
val objektLatitude: Double? = null,
val objektLontitude: Double? = null,
)

The part which throws the error:

 db.collectionGroup(&quot;Houses&quot;).get().addOnSuccessListener { snapshot -&gt;
        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:

从Firestore读取时,我收到了反序列化错误。

答案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&#223;e: Number? = 0,
val objektPreis: Number? = 0,

With These

val objektZimmer: Long? = 0L,
val objektGroe&#223;e: Long? = 0L,
val objektPreis: Long? = 0L,

huangapple
  • 本文由 发表于 2023年1月9日 18:07:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定