Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

dart - Flutter calculate ammount by foreach loop

I have some data and i am simply printing it by forEach loop i need to calculate the price

Here is simple code

allRows.forEach((row) => print(row));

its printing like this

[        ] I/flutter (31845): {id: 1195, title: Fajita Pizza (S),color: Grey, price: 199.0, sizeselect: Small}
[   +1 ms] I/flutter (31845): {id: 1211, title: Tikka Pizza (M),color: Black, price: 349.0, sizeselect: Small}

what i need to do is i need to calculate or sum the price and print the values.

So expected output is 548

question from:https://stackoverflow.com/questions/65872003/flutter-calculate-ammount-by-foreach-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
var amount = 0;
allRows.forEach((row) {
 amount += row.price;
 print(row);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...