How to map an array of objects from Cloud Firestore to a List of objects?
Even from the beginning, the array is a supported data type in Cloud Firestore. It’s very useful for storing data in String format or even as custom objects, as long as the size of the document is less than 1 MiB (1,048,576 bytes). For larger amounts of data, a sub-collection is more likely to be used. However, if we only need small datasets, then storing data in an array is the best option.
Now, let’s assume we have a document that contains a property of type array. This array is named users and holds a few User objects. The User class is very simple, contains only two properties, and looks like this:
And this is the database structure:
So our goal is to get in code the users array as a List<User>. To achieve that, we need to attach a listener on the document and use a get() call: