Reputation: 25
Suppose ${d.price}
has all the prices (which is a double) of items in the list. How can i calculate the total sum of price using ${d.price}
?
Upvotes: 2
Views: 2115
Reputation: 1698
if d.price
has all the prices then
an easy approach to calculate the sum is:
int sum = 0;
for(int i=0; i<d.price.length; i++) {
sum += d.price[i];
}
print( sum );
Updated Code
if you want to show the result you can use Text Widget.
Text( sum.toString() )
Upvotes: 1
Reputation: 1960
void main() {
List<int> a=[10,20,30,40];
int sum=0;
a.forEach((e){
sum+=e;
});
print(sum);
}
o/p:
100
Upvotes: 2
Reputation: 1288
You can use fold
to calculate the sum
Reference
for example :
void main() {
List<int> prices = [10,20,30];
int sum = prices.fold(0, (p, c) => p + c);
print(sum);
}
Output
60
Upvotes: 3