如何在Flutter的ListView.builder中从DocumentSnapshot中对整数长度进行排序

huangapple go评论48阅读模式
英文:

How to sort a length of int from a doccumentSnapshot in a listview.builder Flutter

问题

我已经获取了数组的快照并使用长度进行计数。然后显示了它。但我想在ListView.builder中按从高到低的顺序重新排序它。如何实现?

已支持的结构
如何在Flutter的ListView.builder中从DocumentSnapshot中对整数长度进行排序

到目前为止的代码

//我忘了提到
//这是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?

Backed structure
如何在Flutter的ListView.builder中从DocumentSnapshot中对整数长度进行排序

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?[&#39;usersComment&#39;];
    
                           ListView.builder(
                                      physics: const BouncingScrollPhysics(),
                                      itemCount: usersComment.length,
                                      shrinkWrap: true,
                                      itemBuilder: (context, index) {
                                        return StreamBuilder(
                                            stream: FirebaseFirestore.instance
                                                .collection(&quot;usersComment&quot;)
  //I tried filtered it here But that doesn’t work
                                                .where(usersComment[index])
                                                .snapshots(),
                                            builder: (context,
                                                AsyncSnapshot&lt;
                                                        QuerySnapshot&lt;
                                                            Map&lt;String, dynamic&gt;&gt;&gt;
                                                    snapshot) {
                                              final userComments = snapshot.data!.docs
                                                ..sort((a, b) =&gt; ((b.data()[&#39;vote&#39;]
                                                                as List&lt;dynamic&gt;?)
                                                            ?.length ??
                                                        0)
                                                    .compareTo((a.data()[&#39;vote&#39;]
                                                                as List&lt;dynamic&gt;?)
                                                            ?.length ??
                                                        0));
        
                                              final comment = userComments[index];
                                              final countVote = (comment.data()[&#39;vote&#39;]
                                                          as List&lt;dynamic&gt;?)
                                                      ?.length ??
                                                  0;
                                              if (!snapshot.hasData) {
                                                return Container();
                                              }
                                              return Text(
                                                countVote.toString(),
                                                style: const TextStyle(
                                                    color: Colors.black, fontSize: 15),
                                              );
                                            });
                                      }),

答案1

得分: 1

  1. 移除列表视图。
  2. 在流构建器内返回 listView。
  3. 返回集合中的所有内容(而不是每个文档)。
  4. 在您的流构建器中将所有投票添加到一个列表中。
  5. 对列表进行排序,并使用流内的 ListView 显示。

如何在 Dart 中进行排序

英文:
  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

答案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(&quot;usersComment&quot;).snapshots(),
        builder: (context,
            AsyncSnapshot&lt;QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;&gt;
            snapshot) {
          final userComments = snapshot.data!.docs..sort((a, b) =&gt; ((b.data()[&#39;vote&#39;] as List&lt;String&gt;?)?.length ?? 0).compareTo((a.data()[&#39;vote&#39;] as List&lt;String&gt;?)?.length?? 0));
          ListView.builder(
              itemCount: userComments.length,
              itemBuilder: (context, index){
                final comment = userComments[index];
                final countVote = (comment.data()[&#39;vote&#39;] as List&lt;String&gt;?)?.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(&quot;usersComment&quot;).where(%your condition%).snapshots()

huangapple
  • 本文由 发表于 2023年2月13日 23:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定