Does anyone here know/have references/examples of how to add up the values in the list in Flutter. Thanks
2
use sum:
sum
import 'package:collection/collection.dart'; void main() { final list = [1, 2, 3, 4]; final sum = list.sum; print(sum); // prints 10 }
Your question is similar to the question here, refer to it for more information
you can use .fold() method
.fold()
T fold<T>(T initialValue, T Function(T, Data) combine)
example for sum list of object:
void main() { List<Data> listData = [ Data(count: 10, name: 'a'), Data(count: 12, name: 'bc'), Data(count: 21, name: 'abc'), ]; int sum = listData.fold(0, (int preValue, data) => preValue + data.count); print(sum);// 43 } class Data { int count; String name; Data({required this.count, required this.name}); }
Click here to cancel reply.
2
Answers
use
sum
:Your question is similar to the question here, refer to it for more information
you can use
.fold()
methodexample for sum list of object: