reading data from firebase firestore collection at stream builder

Issue

I got trouble with firebase fireStore.
There is a stream builder reading data from items collection.
Inside items collection there is some fields and another collections.
I haven’t any problem with fields, the problem is with collection.
how to access those collections inside stream builder?

StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
        stream: CallApi().finalReference(reference: widget.finalReference),
        builder: (context, snapshot) {
          if (snapshot.hasError) {
            return Center(child: Text('snapshot Error:${snapshot.error}'));
          }
          if (snapshot.hasData) {
            var snapData = snapshot.data!.docs;
            if (kDebugMode) {
              print(snapData.length);
            }
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child: ListView.builder(
                    itemCount: snapData.length,
                    itemBuilder: (BuildContext context, int index) {
                      return ListItem(
                        mTitle: snapData[index].get('title') ?? '',
                        mSubTitle: snapData[index].get('address') ?? 'empty',
                        mPrice: snapData[index].get('price') ?? '',
                        mImageUrl: snapData[index].get('gallery')[0],
                        mOnTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => DetailsPage(
                                adsTitle: snapData[index].get('title'),
                                adsSubTitle: snapData[index].get('subTitle'),
                                gallery: snapData[index].get('gallery'),
                                specFTitle: snapData[index].get('gallery'),
                              ),
                            ),
                          );
                        },
                      );
                    },
                  ),
                ),
              ],
            );
          }
          return const Center(child: CircularProgressIndicator());
        },
      ),

here is firebase

sssd

Solution

Reading data from Firestore is a shallow operation. When you read a document, its subcollection are not automatically read.

So if you want to get the data from the subcollections of the current document, you will have to start a new read operation for that. If you want to show that data in the UI, you can use a new, nested StreamBuilder or FutureBuilder for that.

Answered By – Frank van Puffelen

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *