我有一个列表视图中显示的类别列表。现在我试图在单击每个类别列表时创建子类别并将其显示到颤动的另一个列表视图中。
对于每个类别,我必须动态创建另一个子类别列表。我有 json 数据和良好的工作类别列表。我必须根据类别列表创建子类别。
我有一个模型类,其中还包含类别详细信息和子类别详细信息。
我怎么能做到这一点?
模型类
class ProductCategoryModel {
String categoryName;
String categoryImage;
String categoryId;
List<SubCategory> subcategory;
ProductCategoryModel(
{this.categoryName,
this.categoryImage,
this.categoryId,
this.subcategory});
factory ProductCategoryModel.fromJson(Map<String, dynamic> json) {
var list = json['children'] as List;
print(list.runtimeType);
List<SubCategory> subCategoryList =
list.map((i) => SubCategory.fromJson(i)).toList();
return ProductCategoryModel(
categoryName: json['name'],
categoryImage: json['image'],
categoryId: json['category_id'],
subcategory: subCategoryList,
);
}
}
class SubCategory {
String subCategoryId;
String subCategoryName;
SubCategory({this.subCategoryId, this.subCategoryName});
factory SubCategory.fromJson(Map<String, dynamic> subJson) {
return SubCategory(
subCategoryId: subJson['SubCategoryModel'],
subCategoryName: subJson['name'],
);
}
}
json响应:
{
"category_id": "1",
"name": "Vehicle",
"column": "1",
"children": [
{
"category_id": "101",
"name": "Two Wheeler",
"product_count": " (0)"
},
{
"category_id": "102",
"name": "Four Wheeler",
"product_count": " (1)"
}
]
},