Summation of value distinct by id in arrayList

Hi I have an arrayList which stores values amount, description and code. I have a function I’m using to get the sum of my amount field which I’m using to do some validations. Below is the sample which gives me the sum irregardless of the code:

var taxableAmountTot = isics.map<int>((e) => int.parse(e.taxableAmount)).reduce((a,b)=>a+b);

What I want is to get the sum based on the code in my arrayList… Anyone with a quick win on how i can go about this?

:upside_down_face:… managed using something like below

var sumOfRents = isics.fold(0,(total, code) => (code.incomeCode == 'xxx') ? code.taxableAmount : total);

Your solution should work just fine, thanks for sharing!

1 Like

Hi @sakina, This works fine, I have only encountered a problem when I try to do comparisons with this same value. So I’m using it as and int like below…

if ((int.parse(sumOfRents) < int.parse(pmt) || (sumOfWHTRents + int.parse(pmt) > int.parse(sumOfRents))) &&  _selectedPaymentCode.toString() == 'xxx')

in some instances validation works just fine but in others I keep getting the error below

type 'int' is not a subtype of type 'String'

Any idea what could be wrong here ?