skip to Main Content

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),
                                              );
                                            });
                                      }),

2

Answers


    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

    Login or Signup to reply.
  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()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search