kharismarizqii
kharismarizqii

Reputation: 58

How to get list of object inside collection from cloud firestore in Android?

Here's the data

How can I get the "listTask"

I've tried to log the "document.data["listTask"]" with the code below:

db.collection("project").get()
        .addOnSuccessListener { result ->
            Log.d(TAG, "onRefresh: loadProject")
            for (document in result){
                if (document.data["owner_id"]== id){
                    counter++
                    Log.d(TAG, "inside response: ${document.data["listTask"]}")
                }
            }

        }

and this is the log result

D/YourProjectFragment: inside response: [{id=29356, title=Database, status=false}, {id=28581, title=UI UX, status=false}, {id=66044, title=Architecture Design, status=false}, {id=77830, title=Functional Test, status=false}]

I don't know the pattern of the response, I have tried with JSONArray but that is not JSONArray.

so how to get the list of objects inside collection in Cloud Firestore?

Upvotes: 3

Views: 1127

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

I have tried with jsonArray but that is not jsonArray

That's obviously not a JSONArray, it's a property of type object. Besides that, JSONArray is not a supported data type.

So how to get the list of objects inside a collection in Cloud Firestore?

Unfortunately, you cannot map your listTask property directly into a list of custom objects. You should write some code for that. The listTask property is actually a list of Map objects, as your response states for:

response: [{id=29356, title=Database, status=false}, {id=28581, title=UI UX, status=false}, {id=66044, title=Architecture Design, status=false}, {id=77830, title=Functional Test, status=false}]

See, it begins with [ and ends with ], this makes it an array and each and every object inside this array is a Map. So you need to iterate through this list of Map objects and create your own list of Task objects.

If you want, you can contact the Firebase team and make a request for that.

Upvotes: 3

Related Questions