Android - Firestore toObject() 无法反序列化具有嵌套字符串数组的对象 [英] Android - Firestore toObject() cannot deserialize an object with a nested Array of Strings

查看:14
本文介绍了Android - Firestore toObject() 无法反序列化具有嵌套字符串数组的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 Firestore 中的 Class 数据模型:

This is my Class data model in Firestore:

这是我的 Kotlin 模型中的 Class.

Here is the Class in my Kotlin model.

data class Class(
    var teacher: String = "",

    val capacity: Int = 0, 

    val location: String = "",

    var roster: List<String> = ArrayList() // this doesn't work and neither does String or HashMap<String, String>
)

我尝试使用协程获取类的数据:

I attempt to grab the data for the class as such using Coroutines:

val snapshot = database.collection("class").get().await()
val class = snapshot.toObject(Class::class.java)

但是,它无法将 roster 映射到 ArrayList 并给我以下堆栈跟踪:

However, it fails to map the roster to an ArrayList and gives me the following stacktrace:

E/FirebaseServiceImpl: getClass: FAILURE java.lang.RuntimeException: 无法反序列化对象.需要一个列表,但得到一个类 java.lang.String(在字段 'roster' 中找到)

E/FirebaseServiceImpl: getClass: FAILURE java.lang.RuntimeException: Could not deserialize object. Expected a List, but got a class java.lang.String (found in field 'roster')

--------- 崩溃开始

--------- beginning of crash

E/AndroidRuntime:致命异常:main

E/AndroidRuntime: FATAL EXCEPTION: main

进程:com.packagename.myapp,PID:8809

Process: com.packagename.myapp, PID: 8809

java.lang.RuntimeException:无法反序列化对象.需要一个列表,但得到一个类 java.lang.String(在字段 'roster' 中找到)

java.lang.RuntimeException: Could not deserialize object. Expected a List, but got a class java.lang.String (found in field 'roster')

我也尝试将 roster 设为 String 和 HashMap,但是当我这样做时,它说它检测到一个 ArrayList.

I've also tried making roster a String and a HashMap but when I do it says it detects an ArrayList.

如果我使用 toObject(),如何获取嵌套在对象中的字符串数组?

How can I grab an array of strings nested within an object if I'm using toObject()?

推荐答案

好的,代码看起来完全没有问题.当我删除该集合并将我的集合的种子数据重新上传到 Firebase 时,它​​解决了我的问题,并且我没有更改代码中的任何内容.不幸的是,我无法说出是什么原因造成的,但如果您遇到类似的问题,重新导入您的数据是否也会有所帮助?

OK, so there was absolutely nothing wrong with the code, it seems. When I deleted the collection and re-uploaded my collection's seed data to Firebase it resolved my issues, and I changed nothing in the code. Unfortunately I can't say what caused it but if you're having a similar issue maybe re-importing your data will help as well?

这篇关于Android - Firestore toObject() 无法反序列化具有嵌套字符串数组的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆