英文:
How to sort a length of int from a doccumentSnapshot in a listview.builder Flutter
问题
我已经获取了数组的快照并使用长度进行计数。然后显示了它。但我想在ListView.builder中按从高到低的顺序重新排序它。如何实现?
到目前为止的代码
//我忘了提到
//这是widget树顶部StreamBuilder的snapShot中的usersCommentId
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")
//我尝试在这里过滤,但不起作用
.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
?.length ??
0)
.compareTo((a.data()['vote'] as List
?.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),
);
});
});
英文:
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),
);
});
}),
答案1
得分: 1
- 移除列表视图。
- 在流构建器内返回 listView。
- 返回集合中的所有内容(而不是每个文档)。
- 在您的流构建器中将所有投票添加到一个列表中。
- 对列表进行排序,并使用流内的 ListView 显示。
英文:
- take out the listview.
- return listView inside the stream builder.
- return everything in the collection (not each doc).
- add all the votes in one list inside your stream builder.
- sort the list and display using the ListView that is inside the stream
答案2
得分: 1
以下是您要求的中文翻译:
由于您已经获取了名为 userComments
的列表,我有一个建议,可以将其整合成一个单一的流查询,如下所示:
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));
return 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),
);
});
});
如果您想筛选 userComments
,则可以使用以下代码:
stream: FirebaseFirestore.instance
.collection("usersComment").where(%您的条件%).snapshots()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论