提供此列表
sourceMap
和班级
[
{
"id": "1",
"name": "Bread",
"sellingPrice": 10
},
{
"id": "2",
"name": "Drink",
"sellingPrice": 30,
}
]
我要在这里得到的是总额。就像下面的代码一样,但是是飞镖。
class Food {
String id;
String name;
double sellingPrice;
}
答案 0 :(得分:3)
您可以在fold
上使用List
方法,也可以将map
与reduce
结合使用:
class Food {
String id;
String name;
double sellingPrice;
}
void main() {
List<Food> foodList = [Food()..sellingPrice = 5, Food()..sellingPrice = 10];
print(foodList.fold<double>(0.0, (sum, food) => sum + food.sellingPrice)); // 15
print(foodList.map((food) => food.sellingPrice).reduce((a,b) => a + b)); // 15
}