1

I have already get the snapShot of array and count it using length. And displayed it. But I want to reorder it from much to lower in a listview.builder. How can I achieve that?

Backed structure enter image description here

Code so far

    //I forgot to mention 
    //This is the usersCommentId from the snapShot of StreamBuilder on top of the widget tree
         final usersComment = snapshot.data?['usersComment'];
    
                           ListView.builder(
                                      physics: const BouncingScrollPhysics(),
                                      itemCount: usersComment.length,
                                      shrinkWrap: true,
                                      itemBuilder: (context, index) {
                                        return StreamBuilder(
                                            stream: FirebaseFirestore.instance
                                                .collection("usersComment")
  //I tried filtered it here But that doesn’t work
                                                .where(usersComment[index])
                                                .snapshots(),
                                            builder: (context,
                                                AsyncSnapshot<
                                                        QuerySnapshot<
                                                            Map<String, dynamic>>>
                                                    snapshot) {
                                              final userComments = snapshot.data!.docs
                                                ..sort((a, b) => ((b.data()['vote']
                                                                as List<dynamic>?)
                                                            ?.length ??
                                                        0)
                                                    .compareTo((a.data()['vote']
                                                                as List<dynamic>?)
                                                            ?.length ??
                                                        0));
        
                                              final comment = userComments[index];
                                              final countVote = (comment.data()['vote']
                                                          as List<dynamic>?)
                                                      ?.length ??
                                                  0;
                                              if (!snapshot.hasData) {
                                                return Container();
                                              }
                                              return Text(
                                                countVote.toString(),
                                                style: const TextStyle(
                                                    color: Colors.black, fontSize: 15),
                                              );
                                            });
                                      }),
It'sPhil
  • 61
  • 1
  • 2
  • 11

2 Answers2

1
  1. take out the listview.
  2. return listView inside the stream builder.
  3. return everything in the collection (not each doc).
  4. add all the votes in one list inside your stream builder.
  5. sort the list and display using the ListView that is inside the stream

how to sort in dart

odinachi
  • 180
  • 1
  • 5
1

As you have already taken all List of userComments, I have a suggestion to make it with in single Stream query as following

StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection("usersComment").snapshots(),
        builder: (context,
            AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>
            snapshot) {
          final userComments = snapshot.data!.docs..sort((a, b) => ((b.data()['vote'] as List<String>?)?.length ?? 0).compareTo((a.data()['vote'] as List<String>?)?.length?? 0));
          ListView.builder(
              itemCount: userComments.length,
              itemBuilder: (context, index){
                final comment = userComments[index];
                final countVote = (comment.data()['vote'] as List<String>?)?.length ?? 0;
                return Text(
                  countVote.toString(),
                  style: const TextStyle(
                      color: Colors.grey,
                      fontSize: 9),
                );
              });
        });

If you want to filter the userComments, then stream: FirebaseFirestore.instance .collection("usersComment").where(%your condition%).snapshots()

Alex Sunder Singh
  • 2,378
  • 1
  • 7
  • 17
  • Thank you so much it worked. But I still have problem with filtering the userComments like the code above(Updated). But its still getting the ['vote'] of the entire collection and I don’t want that. So if you have any idea Plz help – It'sPhil Feb 14 '23 at 14:41
  • If you want to filter, then your collection should have more details about how you want to filter, eg. `createdBy`, `accepted`, `postId` and so on. If this user comment is for post, then You will have postId from, and query will be `where('postId', isEqualTo: postId)` – Alex Sunder Singh Feb 14 '23 at 18:08
  • I don’t quite get it. Can you maybe try update your code? – It'sPhil Feb 15 '23 at 13:23
  • You may have to update the collection for this. – Alex Sunder Singh Feb 15 '23 at 13:30