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}?
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}?
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
void main() {
List<int> a=[10,20,30,40];
int sum=0;
a.forEach((e){
sum+=e;
});
print(sum);
}
o/p:
100
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() )