使用flutter收集值的总和

时间:2019-04-04 15:53:49

标签: dart flutter

我有一个列表/集合,我需要获取此列表中所有价格的总和。

cart_items = [
    {
        "id": 144,
        "created_at": "2019-04-04 14:42:04",
        "updated_at": "2019-04-04 14:42:04",
        "cart_id": "3",
        "client_id": "83",
        "product_id": "6",
        "quantity": "1",
        "price": "1500",
        "name": "Cucumber (2Pcs)",
        "image": "products/es4eGjkgQ6MvzTaMyX4iXWjcSX03mVk3QB9oODWk.jpeg",
       },
    {
        "id": 145,
        "created_at": "2019-04-04 14:42:09",
        "updated_at": "2019-04-04 14:42:09",
        "cart_id": "3",
        "client_id": "83",
        "product_id": "5",
        "quantity": "1",
        "price": "2000",
        "name": "Cauliflower",
        "image": "products/lVZ31zORzltyVIDXhHoCWUgjTlal7cWd7pI8DL2V.jpeg",
        }
]

我一直试图在此收藏夹/列表上使用reduce方法,但是我不知道如何指定我需要获取价格的总和。

1 个答案:

答案 0 :(得分:0)

您在这里:

cart_items.map<int>((m) => int.parse(m["price"])).reduce((a,b )=>a+b)