I want to implement add to checkout in which number of items added is displayed. Plus button adds elements in list and minus removes elements from list. Goal is just to display particular items added and its quantity. I have added items in list want to count length of duplicate items. How we can do that in flutter?
here is your solution. [Null Safe]
void main() {
  List<int> items = [1, 1, 1, 2, 3, 4, 5, 5];
  Map<int, int> count = {};
  items.forEach((i) => count[i] = (count[i] ?? 0) + 1);
  print(count.toString()); // {1: 3, 2: 1, 3: 1, 4: 1, 5: 2}
}
                        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