flutterfirebasedartgoogle-cloud-firestorebilling

About Flutter Firestore Streambuilder/StreamProvider Reads


I'm working on a Flutter project that uses Firestore as the backend, and retrieves data from it in realtime using stream builders and stream providers. As my application needs to be scalable, I want to ensure I start on the right foot when it comes to my structure.

Context The project manages the work schedules of employees, and takes data from 2 collections. 1 has all the staff, the other has all the shifts. By keeping the ID of a staff members shifts in an array within their personal employee doc (stored in the staff collection), I filter down the documents in the collection of every shift to just return the ones that they are allocated to. These are displayed through an item builder that produces cards for each.

The Worry This is working great, however I am concerned that over time the shifts list will fill up and the reads may get out of hand. A solution is to archive old shifts, But i would like the user to be able to scroll back through their past shifts if they need to without needing to navigate to another page or have any other jarring interaction.

The Question I'm aware that many of flutters builder methods are very clever. Listview builders for instance will only build the widgets that are visible on the screen. If i were to use a Stream builder or stream provider, will I be charged for reading every document that my stream picks up, even though its just a stream of QuerySnapshots? Or do those builders recognise that they dont need all the data from the stream and only pick up what's required to build the widgets on the screen currently, and then only accumulate reads as I scroll and more items are built? Additionally, will firestores local data caching keep reads down?

Hopefully my question makes sense, Thanks in advance

I've done a little research. Decided it was worth asking to see if anyone has a clear answer. Sorry if this is a duplicate


Solution

  • Charges is all about snapshots listener and .get, Even without showing it, For example if you have 1000 documents in collection call testCollection, Then if you run:

     db.collection("testCollection").onSnapshot(function(doc) {}); 
    

    First time run this line charge 1000 reads, After that if you run same line again sdk will fetch data from cache and server, And only charges from those documents that have been updated, See Questions about firestore cache and reads charge.

    Listview is about showing data, It does reuse view instances but not doing pagination for you, If you want to charges from those data only showed on screen, Consider use pagination.