首页 > 解决方案 > Flutter - 如何根据List中的ID统计数据

问题描述

我有List<Comment>所有食物。

[
 {
   comment_id: '...'
   food_id: '...'
   comment: '...'
 }, 
 {
   comment_id: '...'
   food_id: '...'
   comment: '...'
 },

  ....

]

我想知道如何计算基于food_idFlutter 的评论数?任何答案都非常有帮助。

标签: androidiosdartflutter

解决方案


您可以使用where(...)过滤列表并.length获取结果长度。

var comments = <Comment>[...];
var count = comments.where((c) => c.product_id == someProductId).toList().length;

推荐阅读