Home > Software design >  Does Streambuilder fetches all the data from a firestore document or only fields that are needed?
Does Streambuilder fetches all the data from a firestore document or only fields that are needed?

Time:10-02

In my App I use a Streambuilder to show a list of plans that are stored in Firestore (every plan has its own document) on a screen:

final CollectionReference planCol = FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser.uid).collection('plans');

body: StreamBuilder(
            stream: planCol.snapshots(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                final planDocs = snapshot.data.docs;
                return ListView.builder(
                  itemCount: planDocs.length,
                  itemBuilder: (context, index) => MyListTile(
                      title: Text(planDocs[index]['name']),
                      trailing: IconButton(
                        icon: Icon(Icons.edit),
                        onPressed: () {
                          edit(planDocs[index]);
                        },
                      ),
                      onTap: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) {
                              return ExerciseTable(
                                plan: UserPlan(
                                  name: planDocs[index]['name'],
                                  exerciseNames: planDocs[index]
                                  ['exerciseNames'],
                                  rows: planDocs[index]['rows'],
                                  planId: planDocs[index].id,
                                ),
                              );
                            },
                          ),
                        );
                      }),
                );
              } else {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
            }));

On this screen, I only want to show the name of each document and not all the other data which is stored in the document(because they are very big). Now I was asking myself how efficient Streambuilder works when each document is very big. Does it load the whole document or only the field of the document, that is needed?

CodePudding user response:

On this screen I only want to show the name of each document and not all the other data which is stored in the document(because they are very big).

All Cloud Firestore listeners fire on the document level. So there is no way you can get only the value of a single field in a document. That's the way Firestore works.

Now I was asking myself how efficient Streambuilder works when each document is very big.

It's more about the time that it takes to download those documents.

Does it load the whole document or only the field of the document, that is needed?

It loads the entire document.

If you need only some names, you should consider storing those names in a single document. In this case, you can read only one document and you have to pay only one read.

  • Related