How to paginate data from multiple Firestore collections in Flutter?

Viewed 46

I want to create a screen in my Flutter app to display all financing details that relate to a specific user. Each kind of financial entry has its own collection on Firestore.

To elaborate more, I have a purchases collection and a transactions collection. A user should be able to see both of these collections together in a single view sorted by date or money-spent, which means (I think) I have to "join" these two collections (like in SQL) and display the result.

The question now remains how can I paginate this data? As far as I know, I can't "join" collections in Firestore, and as far as I have searched I can't really paginate data from multiple collections simultaneously.

1 Answers

If you want to show a paginated view of the data from two collections, you will need to load the full page of data from both collections, merge them in your application code, and discard any data that is more than the page size you want to show.

In cases like this always consider whether you can instead create an additional collection where you store the data from both collections, and read from there. While data duplication is frowned upon in relational data modeling, it is very common in NoSQL databases.

To learn more about the topic, I recommend reading NoSQL data modeling and then watching Todd's excellent Getting to know Cloud Firestore.

Related