Я новичок, чтобы трепетать. Я пытаюсь получить json от API из следующего кода:
final response = await http.get(url, headers: {
'Content-Type': 'application/json; charset=utf-8',
});
print((json.decode(response.body)['categories'] as List)
.map((data) => CategoryModel.fromJson(data))
.toList());
final responseJson = (json.decode(response.body)['categories'] as List)
.map((data) => CategoryModel.fromJson(data))
.toList();
return responseJson;
Однако я не могу десериализовать его. Вот ошибка, которую я получаю:
I/flutter (19749): NoSuchMethodError: The method '[]' was called on null.
I/flutter (19749): Receiver: null
I/flutter (19749): Tried calling: []("products")
CategoryModel:
import 'package:pbl_store/models/cat_assoc.dart';
class CategoryModel{
String id;
String relationShipId;
String name;
String idParent;
String levelDept;
String numberProductRecursive;
String active;
CatAsso associationModel;
CategoryModel({this.id, this.name, this.idParent, this.relationShipId,
this.levelDept, this.associationModel, this.numberProductRecursive, this.active});
factory CategoryModel.fromJson(Map<String, dynamic> parsedJson) {
return CategoryModel(
id: parsedJson['id'].toString(),
name: parsedJson['name'] ,
idParent: parsedJson['id_parent'],
levelDept: parsedJson['level_depth'],
active: parsedJson['active'],
numberProductRecursive: parsedJson['nb_products_recursive'].toString(),
associationModel: CatAsso.fromJson(parsedJson['associations'])
);
}
}
ProductInCategory:
class ProductInCategory{
String id;
ProductInCategory({this.id});
factory ProductInCategory.fromJson(Map<String, dynamic> parsedJson){
return ProductInCategory(
id: parsedJson['id'].toString()
);
}
}
CatAsso:
import 'package:pbl_store/models/product_in_category.dart';
class CatAsso{
List<ProductInCategory> filterPs;
CatAsso({this.filterPs});
factory CatAsso.fromJson(Map<String, dynamic> parsedJson) {
var product = parsedJson['products'];
List<ProductInCategory> filteredProductList = List();
if(product != null){
filteredProductList = List<ProductInCategory>.from(product.map<ProductInCategory>((i) => ProductInCategory.fromJson(i)));
}
return CatAsso(
filterPs: filteredProductList
);
}
}
Внутри каждой категории есть идентификатор подкатегории и идентификатор продукта. Я хочу идентификатор продукта. вот json я пытаюсь получить:
{
"categories": [
{
"id": 2,
"id_parent": "1",
"level_depth": "1",
"nb_products_recursive": "162",
"active": "1",
"id_shop_default": "1",
"is_root_category": "1",
"position": "0",
"date_add": "2018-12-31 09:52:57",
"date_upd": "2018-12-31 09:52:57",
"name": "Home",
"link_rewrite": "home",
"description": "",
"meta_title": "",
"meta_description": "",
"meta_keywords": "",
"associations": {
"categories": [
{
"id": "3"
},
{
"id": "4"
},
{
"id": "5"
}
],
"products": [
{
"id": "8"
},
{
"id": "13"
},
{
"id": "14"
},
{
"id": "17"
},
{
"id": "18"
},
{
"id": "19"
},
{
"id": "125"
},
{
"id": "126"
},
{
"id": "127"
},
{
"id": "128"
},
{
"id": "129"
},
{
"id": "130"
},
{
"id": "131"
},
{
"id": "132"
},
{
"id": "133"
},
{
"id": "134"
},
{
"id": "135"
},
{
"id": "136"
},
{
"id": "137"
},
{
"id": "138"
},
{
"id": "139"
},
{
"id": "140"
},
{
"id": "141"
},
{
"id": "142"
},
{
"id": "143"
},
{
"id": "145"
},
{
"id": "146"
},
{
"id": "147"
},
{
"id": "148"
},
{
"id": "149"
},
{
"id": "150"
},
{
"id": "151"
},
{
"id": "152"
},
{
"id": "153"
},
{
"id": "154"
},
{
"id": "156"
},
{
"id": "157"
},
{
"id": "158"
},
{
"id": "159"
},
{
"id": "160"
},
{
"id": "161"
},
{
"id": "162"
},
{
"id": "163"
},
{
"id": "164"
},
{
"id": "165"
},
{
"id": "166"
},
{
"id": "167"
},
{
"id": "168"
},
{
"id": "169"
},
{
"id": "170"
},
{
"id": "171"
},
{
"id": "172"
},
{
"id": "173"
},
{
"id": "174"
},
{
"id": "175"
},
{
"id": "178"
},
{
"id": "179"
},
{
"id": "180"
},
{
"id": "181"
},
{
"id": "182"
},
{
"id": "183"
},
{
"id": "184"
},
{
"id": "185"
},
{
"id": "186"
},
{
"id": "187"
},
{
"id": "188"
},
{
"id": "189"
},
{
"id": "190"
},
{
"id": "191"
},
{
"id": "192"
},
{
"id": "193"
},
{
"id": "195"
}
]
}
}
]
}
Пожалуйста, объясните мне, почему возникает эта проблема, и помогите мне найти решение ....