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?
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
how to sort in dart
As you have already taken all List of
userComments
, I have a suggestion to make it with in single Stream query as followingIf you want to filter the
userComments
, thenstream: FirebaseFirestore.instance .collection("usersComment").where(%your condition%).snapshots()