How do I access lists within an object of a variable read from a firestore database?

1 day ago 1
ARTICLE AD BOX

This is a question related to parsing list/array data. I can create and update collection documents and I can read the total collection of all documents. Once the collection is read, I can cycle through the result and parse all of the fields with the exception of the "sitePages" field, saved as an array in Firebase firestore. There are 10 instances of this array saved within the sitePages variable. I have included a screen capture of the database structure below.

enter image description here

The following screen capture is how the sitePages variable (List) is stored within the SitesList variable in the app. The sitepages variable is stored as an "Arrays$ArrayList"

enter image description here

Below is a screen capture of how the sitePages array is read from the database. The sitePages variable is read from the database as an "Object[10]" of an "ArrayList". How do I access the data within the elementData object?

enter image description here

How do I access the 10 lists within the elementData object, contained in the result, and transfer the contents to the sitePages list. Any assistance would be appreciated. If there is an alternative way I should be saving or reading the data, I am open to modifying that code too.

I have included the code that I am using to both read and write to the database. I think I have included a sufficient subset.

data class SitesList( var siteId: Int = 0, var siteImage: Int = R.drawable.paraglider1, var siteName: String = "", var sitePages: List<SitePagesList> ) { fun toMap() = mapOf( "siteId" to siteId, "siteImage" to siteImage, "siteName" to siteName, "sitePages" to sitePages ) } data class SitePagesList( var pageId: Int = 0, var pageContent: String = "", var pageImage: String? = null ) { fun toMap() = mapOf( "pageId" to pageId, "pageContent" to pageContent, "pageImage" to pageImage, ) } var sitesList = mutableStateListOf<SitesList>() init { getSitesList() } fun getSitesList() { getSitesListFromDb() } private fun createOrUpdateSite(site: SitesList) { // create or update an individual site from SitesList sitesDb.collection("sites") .document(site.siteId.toString()) .get() .addOnSuccessListener { if (it.exists()) { it.reference.update(site.toMap()) .addOnSuccessListener { // completed successfully } .addOnFailureListener { // error handling logic removed } } else { sitesDb.collection("sites") .document(site.siteId.toString()) .set(site) } } .addOnFailureListener { // error handling logic removed } } private fun getSitesListFromDb() { sitesDb.collection("sites") .get() .addOnCompleteListener { task -> if (task.isSuccessful) { val listOfDocuments = task.result sitesList.clear() for (document in listOfDocuments) { val sitePages = document.data["sitePages"] // used to view contents of the arrayList @Suppress("UNCHECKED_CAST") val list = SitesList( siteId = document.data["siteId"].toString().toInt(), siteImage = document.data["siteImage"].toString().toInt(), siteName = document.data["siteName"].toString(), sitePages = document.data["sitePages"] as List<SitePagesList> ) sitesList.add(list) } } else { // error handling logic removed } } }

Edited with comments/code/logs added as requested:

Here is the code for the log ...

Log.d(TAG, "sitePages => $sitePages") Log.d(TAG, "list.sitePages => ${list.sitePages}")

Here is the result (my apologies for the quantity of text):

2026-02-06 18:31:50.317 19033-19033 ContentValues com.example.cloudbase D sitePages => [{pageImage=null, pageContent=This ridge site ..., pageId=0}, {pageImage=null, pageContent=From Highway 1A, turn west..., pageId=1}, {pageImage=null, pageContent=The site is a ridge, and launching..., pageId=2}, {pageImage=null, pageContent=Top landing at the ridge is..., pageId=3}, {pageImage=null, pageContent=The site itself is designated..., pageId=4}, {pageImage=null, pageContent=Don’t even think..., pageId=5}, {pageImage=null, pageContent=This is the Muller...!, pageId=6}, {pageImage=null, pageContent=, pageId=7}, {pageImage=null, pageContent=https://www.mullerwindsports.com/, pageId=8}, {pageImage=null, pageContent=Local Site Contact:

[email protected]

1(403)932-6760, pageId=9}]

2026-02-06 18:31:50.317 19033-19033 ContentValues com.example.cloudbase D list.sitePages => [{pageImage=null, pageContent=This ridge site is..., pageId=0}, {pageImage=null, pageContent=From Highway 1A, turn west..., pageId=1}, {pageImage=null, pageContent=The site is a..., pageId=2}, {pageImage=null, pageContent=Top landing at..., pageId=3}, {pageImage=null, pageContent=The site itself..., pageId=4}, {pageImage=null, pageContent=Don’t even think..., pageId=5}, {pageImage=null, pageContent=This is the Muller...!, pageId=6}, {pageImage=null, pageContent=, pageId=7}, {pageImage=null, pageContent=https://www.mullerwindsports.com/, pageId=8}, {pageImage=null, pageContent=Local Site Contact:

[email protected]

1(403)932-6760, pageId=9}]

Here is a loop attempting to access the variables in sitePages ...

var index = 0 val size = list.sitePages.size while (index < 10) { Log.d(TAG, "pageId => ${list.sitePages[index].pageId}") Log.d(TAG, "pageImage => ${list.sitePages[index].pageImage}") Log.d(TAG, "pageContent => ${list.sitePages[index].pageContent}") index++ }

Using list.sitePages.size gives me a correct result of 10 but accessing "list.sitePages[index].pageId" crashes.

I've tried .toString().toInt() after pageId.

I've also tried "list.sitePages.toMutableList()[0]" but this also crashes.

- - -

Here is another small code example.

Log.d(TAG, "listOf(list.sitePages[index]) => ${listOf(list.sitePages[0])}") val list2 = listOf(list.sitePages[0]) val tempId = list2[0].pageId

The result of the Log.d is:

listOf(list.sitePages[index]) => [{pageImage=null, pageContent=This ridge site is ..., pageId=0}]

When the line "val tempId = list2[0].pageId" executes the app crashes.

The value of list2 is below - from debug mode.

list2 = {Collections$SingletonList@40522} size = 1 0 = {HashMap@40525} size = 3 "pageImage" -> null key = "pageImage" value = null "pageContent" -> "This ridge site is ..." key = "pageContent" value = "This ridge site is ..." "pageId" -> {Long@40535} 0 key = "pageId" value = {Long@40535} 0
Read Entire Article