I'm building a flutter app where i have to parse some data from api , i set up everything but i'm receiving this error and i don't know why , i'm newbie to flutter , any help would be appreciated thank you .
- Error generated
E/flutter ( 2725): [ERROR:flutter/shell/common/shell.cc(210)] Dart Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FoodModel',
- This is sample of my api response
{
"categories":[
{
"idCategory":"1",
"strCategory":"Beef",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/beef.png",
"strCategoryDescription":"Beef is the
},
{
"idCategory":"2",
"strCategory":"Chicken",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/chicken.png",
"strCategoryDescription":"Chicken is
},
{
"idCategory":"3",
"strCategory":"Dessert",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/dessert.png",
"strCategoryDescription":"Dessert is a course
},
{
"idCategory":"4",
"strCategory":"Lamb",
"strCategoryThumb":"https:\/\/www.site.com\/images\/category\/lamb.png",
"strCategoryDescription":"Lamb, hogget,
}
]
}
- This is how i'm handling the data
class MyApp extends StatelessWidget {
// This widget is the root of your application.
var foodList = new List<FoodModel>();
List<FoodModel> list = new List<FoodModel>();
Future<List<FoodModel>> fetchFoodCategories() async {
var url = "https://www.sitess.com/api/json/v1/1/categories.php";
var response = await http.get(url);
if(response.statusCode == 200) {
foodList.add(json.decode(response.body));
}
return foodList;
}
@override
Widget build(BuildContext context) {
fetchFoodCategories().then((value){
list.addAll(value);
});
- This is model class
class FoodModel {
List<Categories> _categories;
List<Categories> get categories => _categories;
FoodModel({
List<Categories> categories}){
_categories = categories;
}
FoodModel.fromJson(dynamic json) {
if (json["categories"] != null) {
_categories = [];
json["categories"].forEach((v) {
_categories.add(Categories.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
var map = <String, dynamic>{};
if (_categories != null) {
map["categories"] = _categories.map((v) => v.toJson()).toList();
}
return map;
}
}
class Categories {
String _idCategory;
String _strCategory;
String _strCategoryThumb;
String _strCategoryDescription;
String get idCategory => _idCategory;
String get strCategory => _strCategory;
String get strCategoryThumb => _strCategoryThumb;
String get strCategoryDescription => _strCategoryDescription;
Categories({
String idCategory,
String strCategory,
String strCategoryThumb,
String strCategoryDescription}){
_idCategory = idCategory;
_strCategory = strCategory;
_strCategoryThumb = strCategoryThumb;
_strCategoryDescription = strCategoryDescription;
}
Categories.fromJson(dynamic json) {
_idCategory = json["idCategory"];
_strCategory = json["strCategory"];
_strCategoryThumb = json["strCategoryThumb"];
_strCategoryDescription = json["strCategoryDescription"];
}
Map<String, dynamic> toJson() {
var map = <String, dynamic>{};
map["idCategory"] = _idCategory;
map["strCategory"] = _strCategory;
map["strCategoryThumb"] = _strCategoryThumb;
map["strCategoryDescription"] = _strCategoryDescription;
return map;
}
}