Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

Flutter Unhandled Exception: type '(dynamic) => dynamic' is not a subtype of type '(dynamic) => bool' of 'test'

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

this adds all the featured items to featured list

main(List<String> args) {
  List data = api['Categories'];
  var featured = [];

  data.forEach((category) {
    if (category['Subcategories'] != null) {
      category['Subcategories'].forEach((subcategory) {
        subcategory['Items'].forEach((item) {
          if (item['IsFeatured'] ?? false) {
            featured.add(item);
          }
        });
      });
    }
  });

  print(featured);
}

const api = {
    "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
};

output:

[{ID: 1195, Name: Fajita Pizza (S), IsFeatured: true}, {ID: 1196, Name: Fajita Pizza (M), IsFeatured: true}, {ID: 1195, Name: Fajita Pizza (S), IsFeatured: true}]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...