I'm trying to fetch a Firestore document containing an array, however, I can't get the DocumentSnapshot.toObject method to work correctly as soon as I add an array. I don't want to use a collection as the array will likely only contain 1-2 items max.
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]')
Below is my model class
data class SomeModel(
var references : ArrayList<DocumentReference> = ArrayList(0),
var title : String? = null,
var acronym : String? = null,
var number : Long? = null
){
}
My Firestore document contains an array named references with one DocumentReference. If I remove the reference field from the model class the object deserializes just fine.
if i want retrieve list of items inside a single document i could do :-
the data class which contain list of string , the list could be any type
note:- i provide a default values for all parameters , for deserializing it's necessary the class have empty contractor.
data class SomeModel(val references : List<String> = emptyList() , val title : String = "" , val acronym : String = "" , val number : Long = 0L)
then i can point to this single document and deserializing it including the list of itmes in document this way:-
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")
}
}
the data in firestore database :-

the output in my logcat when i runs the above code

Updating my gradle file entry of firebase-firestore from 11.4.2 to 11.6.0 fixed the deserialization issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With