我有一个Web服务,我很好地获得了JSON数据,这部分我没有问题。但是我的JSON很长,我决定将结果对象设置为另一个列表,所以我建立了一个模型并设置它,但是我得到了一个错误,我不知道为什么,因为我有嵌套列表的问题。
我的JSON
我可以读取所有的JSON,但我有一个问题时,设置为另一个列表。
{
"success": true,
"status_code": 200,
"error": null,
"data": {
"message": "my lists",
"has_paginate": 1,
"total_data": 8,
"current_page": 1,
"from": 1,
"to": 8,
"first_page_url": "",
"last_page": 1,
"last_page_url": "",
"next_page_url": "",
"prev_page_url": "",
"path": "http://app.shandizmive.ir/admin/api/v1/categories",
"per_page": 15,
"results": [
{
"id": 35,
"name": "f1",
"description": "dis",
"parent_id": null,
"image_path": null,
"is_active": "1",
"deleted_at": null,
"created_at": "2023-12-19T06:08:09.000000Z",
"updated_at": "2023-12-19T06:08:09.000000Z",
"children": [
{
"id": 36,
"name": "f1_1",
"description": "sub cat",
"parent_id": "35",
"image_path": null,
"is_active": "1",
"deleted_at": null,
"created_at": "2023-12-19T06:08:48.000000Z",
"updated_at": "2023-12-19T06:08:48.000000Z"
},
{
"id": 37,
"name": "f1_2",
"description": "sub cat",
"parent_id": "35",
"image_path": null,
"is_active": "1",
"deleted_at": null,
"created_at": "2023-12-19T09:36:46.000000Z",
"updated_at": "2023-12-19T09:36:46.000000Z"
},
{
"id": 38,
"name": "f1_3",
"description": "sub cat",
"parent_id": "35",
"image_path": null,
"is_active": "1",
"deleted_at": null,
"created_at": "2023-12-20T05:52:48.000000Z",
"updated_at": "2023-12-20T05:52:48.000000Z"
},
{
"id": 39,
"name": "f1_4",
"description": "sub cat",
"parent_id": "35",
"image_path": null,
"is_active": "1",
"deleted_at": null,
"created_at": "2023-12-20T05:53:46.000000Z",
"updated_at": "2023-12-20T05:53:46.000000Z"
},
{
"id": 40,
"name": "f1_5",
"description": "sub cat",
"parent_id": "35",
"image_path": null,
"is_active": "1",
"deleted_at": null,
"created_at": "2023-12-20T06:18:57.000000Z",
"updated_at": "2023-12-20T06:18:57.000000Z"
}
]
}
]
}
}
字符串
我的模型
class CategoryModel {
late List<Results> results;
CategoryModel({
required this.results,
});
CategoryModel.fromJson(Map<String, dynamic> json) {
results =
List.from(json['results']).map((e) => Results.fromJson(e)).toList();
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['results'] = results.map((e) => e.toJson()).toList();
return _data;
}
}
class Results {
Results({
required this.id,
required this.name,
required this.description,
this.parentId,
this.imagePath,
required this.isActive,
this.deletedAt,
required this.createdAt,
required this.updatedAt,
required this.children,
});
late final int id;
late final String name;
late final String description;
late final String? parentId;
late final String? imagePath;
late final String isActive;
late final String? deletedAt;
late final String createdAt;
late final String updatedAt;
late final List<Children> children;
Results.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
description = json['description'];
parentId = json['parent_id'];
imagePath = json['image_path'];
isActive = json['is_active'];
deletedAt = json['deleted_at'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
children =
List.from(json['results']).map((e) => Children.fromJson(e)).toList();
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['id'] = id;
_data['name'] = name;
_data['description'] = description;
_data['parent_id'] = parentId;
_data['image_path'] = imagePath;
_data['is_active'] = isActive;
_data['deleted_at'] = deletedAt;
_data['created_at'] = createdAt;
_data['updated_at'] = updatedAt;
_data['children'] = children.map((e) => e.toJson()).toList();
return _data;
}
}
class Children {
Children({
required this.id,
required this.name,
required this.description,
required this.parentId,
required this.imagePath,
required this.isActive,
required this.deletedAt,
required this.createdAt,
required this.updatedAt,
});
late final int id;
late final String name;
late final String description;
late final String parentId;
late final String? imagePath;
late final String isActive;
late final String? deletedAt;
late final String createdAt;
late final String updatedAt;
Children.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
description = json['description'];
parentId = json['parent_id'];
imagePath = json['image_path'] != null ? json['image_path'] : null;
isActive = json['is_active'];
deletedAt = json['deleted_at'] != null ? json['deleted_at'] : null;
createdAt = json['created_at'];
updatedAt = json['updated_at'];
}
Map<String, dynamic> toJson() {
final _data = <String, dynamic>{};
_data['id'] = id;
_data['name'] = name;
_data['description'] = description;
_data['parent_id'] = parentId;
_data['image_path'] = imagePath;
_data['is_active'] = isActive;
_data['deleted_at'] = deletedAt;
_data['created_at'] = createdAt;
_data['updated_at'] = updatedAt;
return _data;
}
}
型
添加到模型
ModelListCat? modelCat = null;
RxInt len = 0.obs;
Future<void> getCategory(BuildContext context, String link) async {
token = await getToken() ?? "";
modelCat = await ServiceCatList(token!, link).getData(context);
len = modelCat!.data.results.length.obs;
listSlider.clear();
CategoryModel model = CategoryModel(results: modelCat!.data!.results);
update();
}
型
error我在参数类型列表中有一个错误,但我试过了。我只需要将“children”和“results”列表设置为另一个列表,所以我创建了一个模型并将其设置为,但我得到了一个错误“参数类型”列表(其中结果在model_list_cat.dart中定义)“无法分配给参数”
1条答案
按热度按时间qfe3c7zg1#
我创建了一个结果模型,其中的子模型是相同的,然后我使用它。所以希望它能解析json。
字符串