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

查看:41
本文介绍了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)

但是,它无法将花名映射到ArrayList,并为我提供了以下堆栈跟踪:

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

E/FirebaseServiceImpl:getClass:失败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:致命异常:主要

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天全站免登陆