Home > database >  Dart flutter make an array based on similar type of id
Dart flutter make an array based on similar type of id

Time:01-22

I getting a json data array like this.

  final info = [
  {
    "id": "cart1",
    "product": {"id": "01"},
    "seller": {
      "id": "seller1",
    },
    "quantity": 8,
    "amount": 2544,
  },
  {
    "id": "cart2",
    "product": {"id": "02"},
    "seller": {
      "id": "seller1",
    },
    "quantity": 1,
    "amount": 318,
  },
  {
    "id": "cart3",
    "product": {"id": "03"},
    "seller": {
      "id": "seller2",
    },
    "quantity": 1,
    "amount": 200,
  },
];

So I have a list of array like info. Where I have to format that like result so that I can show data ui. I have no Idea how can I that? I am new in flutter please help me .

final result = [
  {
    "seller": "seller1",
    "products": [
      {"productId": "01", "quantity": 8, "amount": 2544},
      {"productId": "02", "quantity": 1, "amount": 318},
    ],
    "price": 2862
  },
  {
    "seller": "seller2",
    "products": [
      {"productId": "03", "quantity": 1, "amount": 200},
    ],
    "price": 200
  },
];

CodePudding user response:

I have created a class having methods to order the data as you want. Just call the method _orderData() from this class:

class Test {
  final info = [
    {
      "id": "cart1",
      "product": {"id": "01"},
      "seller": {
        "id": "seller1",
      },
      "quantity": 8,
      "amount": 2544,
    },
    {
      "id": "cart2",
      "product": {"id": "02"},
      "seller": {
        "id": "seller1",
      },
      "quantity": 1,
      "amount": 318,
    },
    {
      "id": "cart3",
      "product": {"id": "03"},
      "seller": {
        "id": "seller2",
      },
      "quantity": 1,
      "amount": 200,
    },
  ];


  List<Map<String, dynamic>> _orderData() {
    Map<String, List<Map<String, dynamic>>> _map = {};
    List<Map<String, dynamic>> _list = [];
    for (Map<String, dynamic> item in info) {
      String _key = item['seller']['id'];
      if (!_map.containsKey(_key)) {
        _map[_key] = [_getFormattedMap(item)];
      } else {
        _map[_key]?.add(_getFormattedMap(item));
      }
    }
    _map.entries.forEach((element) {
      _list.add(_getFormattedMap2(element));
    });
    print(_list);
    return _list;
  }

  Map<String, dynamic> _getFormattedMap(Map<String, dynamic> item) {
    return {
      'productId': item['product']['id'],
      'quantity': item['quantity'],
      'amount': item['amount'],
    };
  }

  Map<String, dynamic> _getFormattedMap2(
      MapEntry<String, List<Map<String, dynamic>>> entry) {
    return {
      'seller': entry.key,
      'products': entry.value,
      'price': _getTotal(entry),
    };
  }

  num _getTotal(MapEntry<String, List<Map<String, dynamic>>> entry) {
    num total = 0;
    entry.value.forEach((element) {
      total  = element['amount'];
    });
    return total;
  }
}

CodePudding user response:

You can use the groupBy method from the dart:collection library to group the items in the info list by seller. Then, you can iterate through the groups and create a new list with the desired format. Here is an example of how you can do it in dart:

import 'dart:collection';

final groupedBySeller = groupBy(info, (item) => item['seller']['id']);
final result = <Map<String, dynamic>>[];

groupedBySeller.forEach((seller, items) {
  final sellerResult = {
    'seller': seller,
    'products': items.map((item) => {
      'productId': item['product']['id'],
      'quantity': item['quantity'],
      'amount': item['amount'],
    }).toList(),
    'price': items.map((item) => item['amount']).reduce((value, element) => value   element)
  };
  result.add(sellerResult);
});

It will give you final result in the desired format.

  • Related