Umaiz Khan
Umaiz Khan

Reputation: 1227

Flutter add items in array which have condition

I am fetching data from an api, the output of the api is like this:

{
    "Categories": [{
            "ID": 1064,
            "Name": "Pizza",
            "Subcategories": [{
                    "ID": 87,
                    "CategoryID": 1064,
                    "CategoryName": "Pizza",
                    "Items": [{
                            "ID": 1195,
                            "Name": "Fajita Pizza (S)"
                            "IsFeatured": true,
                        },
                        {
                            "ID": 1196,
                            "Name": "Fajita Pizza (M)"
                        },
                        {
                            "ID": 1197,
                            "Name": "Fajita Pizza (L)"
                        }
                    ]
                },
                {
                    "ID": 87,
                    "CategoryID": 1064,
                    "CategoryName": "Pizza",
                    "Items": [{
                            "ID": 1195,
                            "Name": "Fajita Pizza (S)"
                        },
                        {
                            "ID": 1196,
                            "Name": "Fajita Pizza (M)"
                        },
                        {
                            "ID": 1197,
                            "Name": "Fajita Pizza (L)"
                        }
                    ]
                }
            ]
        },
        {
            "ID": 1064,
            "Name": "Pizza",
            "Subcategories": [{
                "ID": 87,
                "CategoryID": 1064,
                "CategoryName": "Pizza",
                "Items": [{
                        "ID": 1195,
                        "Name": "Fajita Pizza (S)"
                        "IsFeatured": true,

                    },
                    {
                        "ID": 1196,
                        "Name": "Fajita Pizza (M)"
                    },
                    {
                        "ID": 1197,
                        "Name": "Fajita Pizza (L)"
                    }
                ]
            }]
        },
        {
            "ID": 1084,
            "Name": "beverages",
            "Description": null,
            "Image": null,
            "StatusID": 1,
            "LocationID": 2112,
            "Subcategories": []
        }
    ],
    "description": "Success",
    "status": 1
}

By foreach loop i am putting all items arrays in one array like this.

  data.forEach((category) {
    if (category['Subcategories'] != null) {
      category['Subcategories'].forEach((subcategory) {
        items['Items'].addAll(subcategory['Items']);
      });
    }
  });

its working also fine now i need to do this to the Items which have "IsFeatured": true, all items array dont have IsFeatured in array only some have. So what i need to do is just add those Items which have IsFeatured True.

Upvotes: 1

Views: 2105

Answers (1)

lyio
lyio

Reputation: 1427

You could have a look at the more functional approaches to accomplish what you are doing. Especially the map and where functions on Dart's Iterable.

Before calling forEach in your inner loop you can call where. This takes a function (like forEach does) that returns either true or false. If the condition is true` the item remains in the list, otherwise it is filtered out.

Something like this:

category['Subcategories']
  .where((subcategory) => subcategory.isFeatured)
  .forEach((subcategory) {
        items['Items'].addAll(subcategory['Items']);
      });

Edit: I didn't realize that the property in question is actually one level down, i.e. part of the item. So combining this with a null-safe access we might get something like:

  data.forEach((category) {
      category['Subcategories']?.forEach((subcategory) {
        items['Items'].addAll(subcategory['Items'].where((s) => s['IsFeatured'] as bool));
      });
  });

You might have to tweak a few things depending on your actual data model and what assumptions you can make about the presence of values. In the example code I assume that IsFeatured is never null. That might not be the case, so look out for errors there.

Upvotes: 2

Related Questions