I have this code that should calculate total sum of a list:
int _counter = 0;
Future getTotal(item) async {
int counter = 0;
_totalPrice.add(int.parse(item));
_totalPrice.forEach((element) => counter += element);
print('LIST: $_totalPrice');
print('SUM: $counter');
return counter;
}
The _counter should be global variable, so I can use it in other functions, but I am getting this strange issue that almost squares after second click of function, here is the result after I click it twice, instead of adding 1 item, the SUM is larger:
As you can see, the result of the SUM should be 312, not 468, for some reason it calculates it wrongly. I just need it to calculate correctly the sum of the list each time the function is invoked.