Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count the total of items in a json(map) list

I have this json in flutter where it contains two list(items) of maps which in turn carry other lists(data). I want to get the sum of the inner list(data) for each combined together. I expect the total to be 3 since the data inside the first list (items) is two + the next contains 1. How can I do that?

{
 
data:[
   "items": {
                "data": [
                    {
                        "id": 874,
                        "user_id": 5,
                        "session_id": null,
                        "merchant_id": 7922,
                        "inventory_id": 701,
                        "product_id": 49405,
                        "batch_number": "767819153",
                        "quantity": 2,
                        "unit_price": 1399,
                        "total_price": 2798,
                        "status_id": 4,
                        "created_at": "2023-02-14T07:02:40.000000Z",
                        "updated_at": "2023-02-14T09:52:27.000000Z",
                        "deleted_at": null,
                        "taxes": [],
                        "tax_amount": 0,
                        "discounts": null,
                        "discount_amount": 0,
                        "is_out_of_stock": false,
                        "is_expired": false,
                    },
                    {
                        "id": 874,
                        "user_id": 5,
                        "session_id": null,
                        "merchant_id": 7922,
                        "inventory_id": 701,
                        "product_id": 49405,
                        "batch_number": "767819153",
                        "quantity": 2,
                        "unit_price": 1399,
                        "total_price": 2798,
                        "status_id": 4,
                        "created_at": "2023-02-14T07:02:40.000000Z",
                        "updated_at": "2023-02-14T09:52:27.000000Z",
                        "deleted_at": null,
                        "taxes": [],
                        "tax_amount": 0,
                        "discounts": null,
                        "discount_amount": 0,
                        "is_out_of_stock": false,
                        "is_expired": false,
                    }
                ],
                "sub_total": 2798,
                "total_tax": 0,
                "total_discount": 0,
                "total_cost": 2798
            },
     "items": {
                "data": [
                    {
                        "id": 874,
                        "user_id": 5,
                        "session_id": null,
                        "merchant_id": 7922,
                        "inventory_id": 701,
                        "product_id": 49405,
                        "batch_number": "767819153",
                        "quantity": 2,
                        "unit_price": 1399,
                        "total_price": 2798,
                        "status_id": 4,
                        "created_at": "2023-02-14T07:02:40.000000Z",
                        "updated_at": "2023-02-14T09:52:27.000000Z",
                        "deleted_at": null,
                        "taxes": [],
                        "tax_amount": 0,
                        "discounts": null,
                        "discount_amount": 0,
                        "is_out_of_stock": false,
                        "is_expired": false,
                    }
                ],
                "sub_total": 2798,
                "total_tax": 0,
                "total_discount": 0,
                "total_cost": 2798
            }
   ]
}

I have tried looping like this but can’t get it.

for (var data in responseData['data']) {
          return data['items']['data'];
        }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

How about the below. Please note the content was changed to be valid Dart object (there area between data and items, to add a curly bracket).

The main point is: whenever a property is known to be a list, cast it with as List and then iterate over its children.

import 'dart:convert';

void main() {
  var total = 0;
  var string = jsonEncode(object);
  var json = jsonDecode(string);
  var data = json['data'] as List;
  for (var i in data) {
    var items = i['items'];
    var innerData = items['data'] as List;
    total += innerData.length;
  }
  print('Total $total');
}

var object = {
  "data": [
    {
      "items": {
        "data": [
          {
            "id": 874,
            "user_id": 5,
            "session_id": null,
            "merchant_id": 7922,
            "inventory_id": 701,
            "product_id": 49405,
            "batch_number": "767819153",
            "quantity": 2,
            "unit_price": 1399,
            "total_price": 2798,
            "status_id": 4,
            "created_at": "2023-02-14T07:02:40.000000Z",
            "updated_at": "2023-02-14T09:52:27.000000Z",
            "deleted_at": null,
            "taxes": [],
            "tax_amount": 0,
            "discounts": null,
            "discount_amount": 0,
            "is_out_of_stock": false,
            "is_expired": false,
          },
          {
            "id": 874,
            "user_id": 5,
            "session_id": null,
            "merchant_id": 7922,
            "inventory_id": 701,
            "product_id": 49405,
            "batch_number": "767819153",
            "quantity": 2,
            "unit_price": 1399,
            "total_price": 2798,
            "status_id": 4,
            "created_at": "2023-02-14T07:02:40.000000Z",
            "updated_at": "2023-02-14T09:52:27.000000Z",
            "deleted_at": null,
            "taxes": [],
            "tax_amount": 0,
            "discounts": null,
            "discount_amount": 0,
            "is_out_of_stock": false,
            "is_expired": false,
          }
        ],
        "sub_total": 2798,
        "total_tax": 0,
        "total_discount": 0,
        "total_cost": 2798
      },
    },
    {
      "items": {
        "data": [
          {
            "id": 874,
            "user_id": 5,
            "session_id": null,
            "merchant_id": 7922,
            "inventory_id": 701,
            "product_id": 49405,
            "batch_number": "767819153",
            "quantity": 2,
            "unit_price": 1399,
            "total_price": 2798,
            "status_id": 4,
            "created_at": "2023-02-14T07:02:40.000000Z",
            "updated_at": "2023-02-14T09:52:27.000000Z",
            "deleted_at": null,
            "taxes": [],
            "tax_amount": 0,
            "discounts": null,
            "discount_amount": 0,
            "is_out_of_stock": false,
            "is_expired": false,
          }
        ],
        "sub_total": 2798,
        "total_tax": 0,
        "total_discount": 0,
        "total_cost": 2798
      }
    }
  ]
};

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading