I am fetching data from api something 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)"
"IsFeatured": true
},
{
"ID": 1197,
"Name": "Fajita Pizza (L)",
"IsFeatured": false,
}
]
},
{
"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
}
What i need to do is add items in an array which is also working. But i need to do is i need to add those items which have "IsFeatured": true
I got answer on my other post which is this
data.forEach((category) {
if (category['Subcategories'] != null) {
category['Subcategories']
.where((subcategory) => subcategory.isFeatured)
.forEach((subcategory) {
featured['Featured'].addAll(subcategory['Items']);
});
}
});
print(featured);
Its showing this error in this answer
Unhandled Exception: type '(dynamic) => dynamic' is not a subtype of type '(dynamic) => bool' of 'test'
Adding my whole function code
Future<List> dosomestuff() async {
http.Response res = await http.get(
'http://retailapi.airtechsolutions.pk/api/menu/2112',
);
Map<String, dynamic> map = json.decode(res.body);
if (map['description'] == "Success") {
print('show kr ');
List<dynamic> data = map["Categories"];
data.forEach((category) {
if (category['Subcategories'] != null) {
category['Subcategories'].forEach((subcategory) {
items['Items'].addAll(subcategory['Items']);
});
}
});
data.forEach((category) {
if (category['Subcategories'] != null) {
category['Subcategories'].forEach((subcategory) {
subcategory['Items'].forEach((item) {
if (item['IsfeatureProduct'] ?? false) {
featureProduct.add(item);
}
});
});
}
});
print('featureProduct');
print(featureProduct);
print(items["Items"].length);
print(map["Categories"].length);
print(map["Categories"]);
cat['Cat'].addAll(map['Categories']);
print(cat);
setState(() {
show = true;
});
}
}
question from:
https://stackoverflow.com/questions/65900646/flutter-unhandled-exception-type-dynamic-dynamic-is-not-a-subtype-of-typ