Getting firestore collection array in to data entity

I'm new in Android Kotlin and I want to GET an array of help in Firestore into my entity and this is my code:

val appCollection = App.context.db.collection("app")
    val docApp = appCollection.document("info")
    docApp.get().addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val document = task.result
            if (document.exists()) {
                val list = document["help"] as List<*>?
            }
        }
    }

how can i put that list into my entity, and this is my Entity

class Info() : Parcelable{

var help: List<Help>? = null
var howto: List<HowTo>? = null
var info: List<Info>? = null

enter image description here

Upvotes: 2

Views: 71

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

As I see in your screenshot, your help array contains objects of type String and not objects of type Help. To read such an array, please change the following field declaration from:

var help: List<Help>? = null

To:

var help: List<String>? = null

If you however need to store Help in the array, then you need to first add such objects accordingly. So your structure might look like this:

Firestore-root
  |
  --- app (collection)
       |
        --- info (document)
             |
             --- help (array)
                  |
                  --- 0
                      |
                      --- fieldOne: valueOne
                      |
                      --- fieldTwo: valueOTwo

To map such an array to a List of custom objects, please check the following article:

Upvotes: 1

Related Questions