0

I have list, I want to get total number of quantities available

var itemlist=[{"item_name":"32inch Tv","barnd":"samsung","qty":12},
              {"item_name":"32inch Tv","barnd":"realme","qty":10},
              {"item_name":"55inch Tv","barnd":"samsung","qty":10},
              {"item_name":"55inch Tv","barnd":"mi","qty":36},
              {"item_name":"40inch Tv","barnd":"sony","qty":20},
              {"item_name":"40inch Tv","barnd":"vu","qty":12}];

in dart how to get sum of "qty" based on "item_name"?

like

32inch Tv =22
55inch Tv =46
40inch Tv =32

how to get this result???

as

 List<int> qty=[22,46,32];
2
  • It is not clear which way you want the answer. Do you want it as key/value aka 32inch Tv =22, or just the qty number like as 22? Commented Mar 2, 2022 at 14:49
  • i want list like qty=[22,46,32]; Commented Mar 2, 2022 at 14:51

2 Answers 2

3

What you need to do is, collect all in a map as a key value pair and get the quantities at the end by adding one by one.

void main() {
  final itemlist = [{"item_name":"32inch Tv","barnd":"samsung","qty":12},
              {"item_name":"32inch Tv","barnd":"realme","qty":10},
              {"item_name":"55inch Tv","barnd":"samsung","qty":10},
              {"item_name":"55inch Tv","barnd":"mi","qty":36},
              {"item_name":"40inch Tv","barnd":"sony","qty":20},
              {"item_name":"40inch Tv","barnd":"vu","qty":12}];
  
  final values = <String, int>{};
  
  for (int i = 0; i < itemlist.length; i++) {
    final item = itemlist[i];
    final itemName = item['item_name'] as String;
    final qty = item['qty'] as int;
    int previousValue = 0;
    if (values.containsKey(itemName)) {
      previousValue = values[itemName]!;
    }
    previousValue = previousValue + qty;
    values[itemName] = previousValue;
  }
  
  final result = values.values;
  print('result: $result');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you sir got the result.. thank you all lot..
Awesome, if it helped, accept the answer so it can help the other folks who has similar questions ;)
1
void main() {
  var itemlist=[{"item_name":"32inch Tv","barnd":"samsung","qty":12},
              {"item_name":"32inch Tv","barnd":"realme","qty":10},
              {"item_name":"55inch Tv","barnd":"samsung","qty":10},
              {"item_name":"55inch Tv","barnd":"mi","qty":36},
              {"item_name":"40inch Tv","barnd":"sony","qty":20},
              {"item_name":"40inch Tv","barnd":"vu","qty":12}];
  var result = itemlist
     .where((e) => e['item_name'] == '55inch Tv')
     .map((e) => e['qty'] as int)
    .reduce((x,y) => x+y);
  print(result);
}

Output:

46

2 Comments

thank you ,, but i want list like qty=[22,46,32];
Then in the code above do not use reduce and is done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.