1

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

1 Answer 1

1

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.

2
  • Useing nested StreamBuilder dosn't effect on apps' speed? Dec 21, 2022 at 5:56
  • The nested StreamBuilders don't really make a difference, but the extra reads might of course. With your current data structure there's not much different you can do though. If the performance is impact, consider merging information you need for the current screen into fewer documents - maybe even all in the parent document. Dec 21, 2022 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.