I have a List<Comment>
from all foods.
[
{
comment_id: '...'
food_id: '...'
comment: '...'
},
{
comment_id: '...'
food_id: '...'
comment: '...'
},
....
]
I want to know how to count the number of comments based on food_id
in Flutter?
Any answer is very helpful.
You can use where(...)
to filter a list and .length
the get the result length.
var comments = <Comment>[...];
var count = comments.where((c) => c.product_id == someProductId).length;
This code will iterate over the comments and obtain the number of comments which are about the product_id_param specified.
int number_of_comments = comments.fold(
0,
(sum, comment) => (comment.product_id == product_id_param) ? sum + 1 : sum
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With