1

i have single list but i want to create new list having elements

  var products=[
              {"pro_id": "AZ101","quality":"top", "model": "M-Plaz","price": 22500},
              {"pro_id": "AZ101","quality":"top", "model": "Z-Plaz","price": 33500},
              {"pro_id": "AB102","quality":"middel", "model": "M-Neo", "price": 11560},
              {"pro_id": "AB102","quality":"middel", "model": "N-Neo1","price": 13600}];

i want output result like below,

under products want.. pro_id,quality and items based on pro_id

var newlist=[{"products":[
    {
        "pro_id":"AZ101",
        "quality":"top",
        "items": [
           {"pro_id": "AZ101","quality":"top", "model": "M-Plaz","price": 2500},
          {"pro_id": "AZ101","quality":"top", "model": "Z-Plaz","price": 3500}
        ]},{
        "p_id": "AB102",
        "quality":"middel",
        "items": [
           {"pro_id": "AB102","quality":"middel", "model": "M-Neo", "price": 1560},
          {"pro_id": "AB102","quality":"middel", "model": "N-Neo1","price": 3600}
        ]},
    ]
}];

How get this result.

2

2 Answers 2

1

First, I am retrieving pro_id, then finding product based on it. You can find info on code-comment.

Run on dartPad.

void main() {
  final List<Map<String, dynamic>> products = [
    {"pro_id": "AZ101", "quality": "top", "model": "M-Plaz", "price": 22500},
    {"pro_id": "AZ101", "quality": "top", "model": "Z-Plaz", "price": 33500},
    {"pro_id": "AB102", "quality": "middel", "model": "M-Neo", "price": 11560},
    {"pro_id": "AB102", "quality": "middel", "model": "N-Neo1", "price": 13600}
  ];

  /// filter product ids
  List<String> productIds = [];
  List<String> qualites = [];

  products.forEach((product) {
    if (!productIds.contains(product['pro_id'])) {
      productIds.add(product['pro_id']);
      qualites.add(product['quality']);
    }
  });

  // print(productIds.toString());

  /// create a subResult that will hold List of product based on id
  List<Map<String, dynamic>> subResult = [];

  /// find product by id
  List<Map<String, dynamic>> _getProducts(String id) {
    final filterProduts =
        products.where((element) => element['pro_id'] == id).toList();

    // print(filterProduts);
    return filterProduts;
  }

  for (int i = 0; i < productIds.length; i++) {
    subResult.add({
      "pro_id": productIds[i],
      "quality": qualites[i],
      "items": _getProducts(productIds[i]),
    });
  }

  // print(subResult);

  /// for finish result wrap with map and list, then can be convert to List
  final List<Map<String, dynamic>> result = [
    {"products": subResult}
  ];

  print(result);
}
Sign up to request clarification or add additional context in comments.

4 Comments

you can also check about Map to List
Thank you Brother, I have searched so many posts.. you helped..
Glad to help you out. I will give a try, and we thank by accepting and up voting.
Can you help me with this also... stackoverflow.com/questions/71296560/… I tried to paste code but im getting error while posting so i uploded image only.. thank you
0

i'm not sure what you want. but maybe this help you.

Map<String, dynamic> newList = {'products' : products};

2 Comments

It is more like comment and doesn't answer the question
i have list called "products" and I want to create new list Like "newlist".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.