Home > OS >  How to sum up value of an object inside list inside of an object
How to sum up value of an object inside list inside of an object

Time:09-30

i have this Json which i want to sum the price value, and everytime user add new group_object the sum is updated

{"data": [
{
  "group_object": {
    "id": "1",
    "seller": "seller1",
    "list_object": [
      {
        "id": "1",
        "item_name": "Seller_1_item_1",
        "price": "10"
      },
      {
        "id": "2",
        "item_name": "Seller_1_item_2",
        "price": "5"
      }
    ]
  }
},
{
  "group_object": {
    "id": "2",
    "seller": "seller2",
    "list_object": [
      {
        "id": "1",
        "item_name": "Seller_2_item_1",
        "price": "15"
      },
      {
        "id": "2",
        "item_name": "Seller_2_item_2",
        "price": "3"
      }
    ]
  }
}

I tried to use foreach of group_object but still couldn't get it right.

list["data"].forEach((group) {
    group["list_object"].forEach((item) {
      _sum  = item["price"];
    });
  });

when this code run for the first time when _sum is 0 it get its value right but when user add new item into the list and this code run again, it sum the previous value with the new value it gets from looping the whole list

CodePudding user response:

This will do the operation. Use jsonDecode on response.

void main(List<String> args) {
  /// final jsonData = jsonDecode(response.body);
  final group_object = jsonData["data"] as List;
  double result = 0;
  for (final obj in group_object) {
    final items = obj["group_object"]["list_object"] as List;

    for (final item in items) {
      result  = int.tryParse(item["price"]) ?? 0;
    }
  }

  print(result);
}


final jsonData = {
  "data": [
    {
      "group_object": {
        "id": "1",
        "seller": "seller1",
        "list_object": [
          {"id": "1", "item_name": "Seller_1_item_1", "price": "10"},
          {"id": "2", "item_name": "Seller_1_item_2", "price": "5"}
        ]
      }
    },
    {
      "group_object": {
        "id": "2",
        "seller": "seller2",
        "list_object": [
          {"id": "1", "item_name": "Seller_2_item_1", "price": "15"},
          {"id": "2", "item_name": "Seller_2_item_2", "price": "3"}
        ]
      }
    }
  ]
};

More about fetch-data

CodePudding user response:

First define a class model:

class ItemModel {
  final String seller;
  final List<ObjectModel> objects;

  ItemModel({required this.seller, required this.objects});
  static ItemModel fromJson(Map<String, dynamic> json) {
    var object = json['list_object'] as List;
    return ItemModel(
        seller: json['seller'],
        objects: object.map((e) => ObjectModel.fromJson(e)).toList());
  }
}

class ObjectModel {
  final int price;
  final String name;

  ObjectModel({required this.price, required this.name});
  static ObjectModel fromJson(Map<String, dynamic> json) {
    return ObjectModel(
        price: int.parse(json['price']), name: json['item_name']);
  }
}

then use it like this:

void main() {
  var data = response['data'] as List;

  var sum = 0;
  for (var element in data) {
    var group = ItemModel.fromJson(element["group_object"]);
    for (var item in group.objects) {
      sum = sum   item.price;
    }
  }

  print("sum = $sum");
}

the response is your json.

CodePudding user response:

Use for loop for iterating the price values and add up to total sum.

Suppose jsonData is your Json Data;

for(var i in jsonData["data"]!){
    List priceItems = i["group_object"]!["list_object"] as List;

    for(var j in priceItems){
      totalSum  = int .tryParse(j["price"]) ?? 0;
    }
  }

CodePudding user response:

Re-initialize the sum variable before running the loop on list again.

  • Related