4

What's the best way to sum all the "amounts" of all "expenses", for each user?

I've tried a few different things but I can't quite get it right. It should return 2 values: 20.0 and 24.90

[
  {
    "id": 3,
    "company": {
      "id": 2
    },
    "user": {
      "id": 3,
      "first_name": "Fred",
      "last_name": "Smith",
      "email": "asdfasf",
      "is_suspended": false,
      "vendor_id": "FS-100",
      "username": "etytyurtyu",
      "expense": [
        {
          "id": 7,
          "date": "2019-12-14T00:00:00.000Z",
          "amount": 20.0,
          "payment_type": "companyAccount",
          "last_modified": "2019-12-16T23:50:00.459064Z",
          "receipt_uri": [
            "URL8",
            "URL9"
          ],
          "user": {
            "id": 3
          },
          "expense_type": {
            "id": 4
          },
          "booking": {
            "id": "HI-3565346"
          }
        }
      ]
    }
  },
  {
    "id": 2,
    "company": {
      "id": 2
    },
    "user": {
      "id": 2,
      "first_name": "Pierre",
      "last_name": "XXXMar",
      "email": "asdfasdfads",
      "is_suspended": false,
      "vendor_id": "PM-100",
      "username": "asdfas",
      "expense": [
        {
          "id": 2,
          "date": "2019-12-16T00:00:00.000Z",
          "amount": 12.45,
          "payment_type": "provided",
          "last_modified": "2019-12-16T19:01:37.092932Z",
          "receipt_uri": [
            "URL1"
          ],
          "user": {
            "id": 2
          },
          "expense_type": {
            "id": 6
          },
          "booking": {
            "id": "MU-123414"
          }
        },
        {
          "id": 5,
          "date": "2019-12-08T00:00:00.000Z",
          "amount": 12.45,
          "payment_type": "provided",
          "last_modified": "2019-12-16T23:50:00.459064Z",
          "receipt_uri": [
            "URL1"
          ],
          "user": {
            "id": 2
          },
          "expense_type": {
            "id": 6
          },
          "booking": {
            "id": "MU-123414"
          }
        },
        {
          "id": 3,
          "date": "2019-12-17T00:00:00.000Z",
          "amount": 20.0,
          "payment_type": "companyCard",
          "last_modified": "2019-12-16T19:01:37.092932Z",
          "receipt_uri": [
            "URL5",
            "URL6"
          ],
          "user": {
            "id": 2
          },
          "expense_type": {
            "id": 12
          },
          "booking": {
            "id": "HI-3565346"
          }
        }
      ]
    }
  }
]

Thanks

Lee
  • 1,389
  • 3
  • 18
  • 28

2 Answers2

8

You would first map the expenses amounts, then fold it into a single value, like this :

double sum = expenses.map((expense) => expense.amount).fold(0, (prev, amount) => prev + amount);
MickaelHrndz
  • 3,604
  • 1
  • 13
  • 23
  • I tried this, but it didn't work. I assume that's because each user has a list of expense. – Lee Jan 05 '20 at 02:46
  • My bad. The way I wrote it is if you serialize json inside a model class. Here is the right, tested code : `for(var profile in jsonDecode(json)) Text(profile['user']['expense'].map((expense) => expense['amount']).fold(0.0, (prev, amount) => prev + amount).toString())` – MickaelHrndz Jan 05 '20 at 03:28
  • 3
    For the sake of completeness: In 2021 you can replace the `.fold(0, (prev, amount) => prev + amount)` part by just calling `.sum` on the list. Just import `import 'package:collection/collection.dart';` first – tmaihoff Jul 31 '21 at 15:10
0
  1. List item

For the sake of completeness: In 2021 you can replace the .fold(0, (prev, amount) => prev + amount) part by just calling .sum on the list. Just import import 'package:collection/collection.dart'; first – tmaihoff

meet
  • 1