skip to Main Content

How can I define the data model in Flutter for documents in Firestore that have subcollections?

Let’s say I have documents of Books and Books have subcollections chapters in Firestore. In many videos on Firestore, they compare arrays with subcollections. That confuses me a little bit.

This is my approach:

class Book{
    String title,
    String author,
    String year,
    List<Chapter> chapters,
    ...
  }

Once I read the data from Firestore I want to parse them into Books and chapters (using the data model).
Will this work or will I run into some issues?

2

Answers


  1. You’ll need two read operations:

    1. To read the book document itself
    2. To read the chapters for the book

    For each of those you then need to map the document data that you get back to the data model in your code.

    So to read a book and its chapters, you’d read the book document, read the chapters, and then map the results to the data model.

    Login or Signup to reply.
  2. How can I define the data model in Flutter for documents in Firestore that have subcollections?

    If you have a database schema that looks like this:

    db
    |
    --- books (collection)
         |
         --- $bookId (document)
              |
              --- //Book fields
              |
              --- chapters (sub-collection)
                   |
                   --- $chapterId (document)
                         |
                         --- //Chapter fields
    

    Then please note, that there is no way you can perform a single query to get documents that can be directly mapped into objects of the following class:

    class Book{
        String title,
        String author,
        String year,
        List<Chapter> chapters,
    }
    

    Why? That’s because Qqeries in Firestore are shallow, meaning they only get documents from the collection that the query is run against. There is no way to get documents from a top-level collection and a sub-collection in a single query. A single query may only use fields of documents in a single collection. So as @FrankvanPuffelen already mentioned in his answer, you have to perform two separate queries, one to get the books and one to get the corresponding chapters, and right after that join the result on the client.

    In many videos on Firestore, they compare arrays with subcollections. That confuses me a little bit.

    There is however a solution in which you can keep the actual data class model, but instead of using a sub-collection for chapters, you can use an array. The database schema should look like this:

    db
    |
    --- books (collection)
         |
         --- $bookId (document)
              |
              --- //Book fields
              |
              --- chapters (array)
                   |
                   --- [chapterObj1, chapterObj2]
    

    In this way, you’ll be able to perform a query against the sub-collection and get the books together with the chapters in one go. Such a solution will work, only if the size of the document stays < 1 Mib which is the maximum limitation in the size of a document.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search