1

I am trying to build a review app. I am trying to filter out reviews that were hidden by user, so I have a following streamBuilder class:

return StreamBuilder(
  stream: reviewRef.snapshots(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      final documents =
      snapshot.data.docs.where((snapshot) => snapshot.data() 
        ["hidingUserId"] == null ||
          snapshot.data()['isHiddenBy'] != 
        FirebaseAuth.instance.currentUser.uid).toList();
      if (documents.length == 0) {
        return Padding(
          padding: const EdgeInsets.only(top: 100.0),
          child: Container(
            height: 60.0,
            width: 100.0,
            child: Center(
              child: Text(language?.noReviews ?? ""),
            ),
          ),
        );
      } else {
        return ListView.builder(
          padding: padding,
          scrollDirection: scrollDirection,
          itemCount: documents.length,
          shrinkWrap: shrinkWrap,
          physics: physics,
          itemBuilder: (BuildContext context, int index) {
            return itemBuilder(context, documents[index]);
          },
        );
      }
    } else {
      return circularProgress(context);
    }
  },
);

and then in my readReviews class where I have 5 swipable fragments where all reviews are displayed, by a streamBuilderWrapper, where I sort the stream, and display reviews according to some order, for instance ratings = stars, or likes:

StreamBuilderWrapper(
      shrinkWrap: true,
      stream: reviewRef
          .orderBy('stars', descending: true)
          .orderBy('timestamp', descending: true)
          .snapshots(),
      physics: NeverScrollableScrollPhysics(),
      itemBuilder: (_, DocumentSnapshot snapshot) {
        Review review= Review.fromJson(snapshot.data());
        return review.reviewId != null
            ? Padding(
          padding: const EdgeInsets.only(bottom: 12.0),
          child: Review(review: reviews),
        )
            : Container(
          child: Center(
            child: Text('blahblahblah'),
          ),
        );
      },
    ),

However, my sorting in StreamBuilderWrapper does not work. Posts show some random order, same on all five fragments. Hiding posts, finally works though! Why is ordering not working? thank you for the help.

HaKim
  • 277
  • 2
  • 12
  • Hi, have you created [Composite indexes](https://firebase.google.com/docs/firestore/query-data/index-overview#composite_indexes) in the Firebase? – Zeenath S N Nov 07 '21 at 07:29
  • Hi! Yes, i do have all indexes. And they were working. But, after I added filtering out conditions, orders stopped working – HaKim Nov 07 '21 at 09:29
  • Can you refer these stackoverflow answers and let me know if it helps? https://stackoverflow.com/a/65956980/15774177, https://stackoverflow.com/questions/51434167/firestore-query-orderby-not-working?rq=1 – Zeenath S N Nov 09 '21 at 11:03

0 Answers0