反序列化包含数组的Firestore文档的推荐方法是什么?

我试图获取包含数组的Firestore文档,但是,我无法使DocumentSnapshot.toObject方法在我添加数组后立即正常工作。 我不想使用集合,因为数组可能最多只包含1-2个项目。

 java.lang.RuntimeException: Could not deserialize object. Failed to convert value of type com.google.android.gms.internal.zzegf to DocumentReference (found in field 'references.[0]') 

以下是我的模特班

 data class SomeModel( var references : ArrayList<DocumentReference> = ArrayList(0), var title : String? = null, var acronym : String? = null, var number : Long? = null ){ } 

我的Firestore文档包含一个名为references的数组,其中包含一个DocumentReference 。 如果我从模型类中删除引用字段,对象反序列化就好了。

如果我想检索单个文件内的项目列表,我可以做到:

包含字符串列表的数据类,列表可以是任何类型

注意: – 我提供了所有参数的默认值,反序列化是必要的类有空承包商。

  data class SomeModel(val references : List<String> = emptyList() , val title : String = "" , val acronym : String = "" , val number : Long = 0L) 

那么我可以指向这个单一的文件,并反序列化它包括在这个文件中的文件列表: –

 val db = FirebaseFirestore.getInstance() val someModelRef = db.collection("someCollection").document("SomeModel") someModelRef.get().addOnSuccessListener { val someModel : SomeModel = it.toObject(SomeModel::class.java) someModel.references.forEach { Log.i("MainActivity","items $it") } } 

firestore数据库中的数据: –

在这里输入图像描述

当我运行上面的代码在我的logcat的输出

在这里输入图像描述