Snapshot has data even if it not exist in Firestore

Viewed 19

I have this StreamBuilder:

 SizedBox(
                  height: 200,
                  child: StreamBuilder<QuerySnapshot>(
                    stream: FirebaseFirestore.instance
                        .collection('users')
                        .doc(uid)
                        .collection('favoritePets')
                        .snapshots(),
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        print(snapshot);
                        return ListView.builder(
                            itemCount: snapshot.data!.docs.length,
                            itemBuilder: (context, index) {
                              DocumentSnapshot doc = snapshot.data!.docs[index];
                              return Text(
                                doc['title'],
                              );
                            });
                      } else {
                        return Text(
                          "No data",
                        );
                      }
                    },
                  ),
                ),

If the collection does not exist in the Firestore because no items were added or all of the items are removed I got a blank page, instead of showing the "No data" text.

This gets printed no matter whether the collection exists or not:

AsyncSnapshot<QuerySnapshot<Object?>>(ConnectionState.active, Instance of '_JsonQuerySnapshot', null, null)

How is it possible to get the "No data" text?

0 Answers
Related