Aamir Zaidi
Aamir Zaidi

Reputation: 5

No of reads while getting List<DocumentSnapshots> in Flutter using Cloud Firestore

I am new to firestore and flutter and i was wondering certain things about read and write operations

  1. if inside the StreamBuilder we store the list of DocumentSnapshots in a variable like this, final events = snapshot.data.documents;

    then would the no of read operations equal to all the document inside the collection, or just one?

  2. When we add a new document from the firestore console using the StreamBuilder, does it results in one more read operation or the whole Stream builder rebuilds after taking a write, hence resulting in all the reads as before plus one.

Sorry if my question is not very clear. Here is my code for the StreamBuilder.

  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: fireStore.collection('trending_events').snapshots(),
      builder: (BuildContext context, AsyncSnapshot snapshot){
        if(!snapshot.hasData){
          return CircularProgressIndicator();
          }
        if(snapshot.hasError){
            return Center(child: Text('Something Wrong'),);
          }
          final events = snapshot.data.documents;
          final urlList = [];
          for(var eventUrl in events){
            final url = eventUrl.data['video_url'];
            urlList.add(url);
          }
          return Padding(
            padding: const EdgeInsets.only(left: 10.0, right: 10.0, top: 23.0),
            child: Column(
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                IconButton(
                  icon: Icon(Icons.gesture, size: 20.0,color: Colors.black,),
                  onPressed: (){
                    Navigator.push(context, MaterialPageRoute(
                      builder: (context) => UpcomingEvents(),
                    ));
                  },
                ),
                SizedBox(height: 4.0,),
                Expanded(
                  child: ListVideoGenerator(listURL: urlList),
                ),
              ],
            ),
          );
      },
    );
  }```


Upvotes: 0

Views: 280

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

then would the no of read operations equal to all the document inside the collection, or just one?

If inside the collection, there is 4 documents then you will be charged for those 4 documents.

When we add something from the firestore console using the StreamBuilder, Is only one document added resulting in one more read operation or the whole Stream builder rebuilds after taking a write, hence resulting in all the reads as before plus one.

It will be one read, since firestore uses cache by default. According to the docs:

Cloud Firestore allows you to listen to the results of a query and get realtime updates when the query results change.

When you listen to the results of a query, you are charged for a read each time a document in the result set is added or updated. You are also charged for a read when a document is removed from the result set because the document has changed. (In contrast, when a document is deleted, you are not charged for a read.)

Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.

Upvotes: 1

Related Questions