Home > database >  Flutter / Firebase Firestore : How to create a book tracking system?
Flutter / Firebase Firestore : How to create a book tracking system?

Time:10-03

I have an app in Flutter where users can add some book chapters to their profile and read it. Now I want to create a training system when the user has read a chapter, the app marks it as read. I'm using Firebase Firestore like this Firestore model

I was thinking to add a "read" field in the chapter document but it will result in every user will have the mark "read" in the app.

So, how to create a tracking system that allows marking read chapters just for the current user?

CodePudding user response:

The best option that you have is to store all those chapters in an array in the user object:

$uid (document)
 |
 --- chapters: ["Cross Guild", "Other Cross Guild"]

Or in a Map:

$uid (document)
 |
 --- chapters (map)
       |
       --- Cross Guild: true
       |
       --- Other Cross Guild: true

I will have 10.000 chapters.

If you're afraid about the 1 MiB maximum limitation of a document, then I recommend you create a document for the starting letter of a chapter:

Firestore-root
  |
  --- chapters (collection)
        |
        --- aChapters (document)
        |     |
        |     --- //Array or Map of chapters starting with the letter a.
        |
        --- bChapters (document)
              |
              --- //Array or Map of chapters starting with the letter b.

In this way, you'll be able to store 26 MiB of data, which is pretty much.

CodePudding user response:

You need to create such field for users collection. For example, creating "read chapters" named field (readed is not grammatically correct) which is an array, accepting String. If a user read, then you add this chapter's name to this user's doc(to "read chapters" field I mean). So you can check like that, array.contains("chapter_name") and mark the chapter as read or unread. That is all.

  • Related