无法将嵌套JSON分配给我的结构[Flutter]

svujldwt  于 4个月前  发布在  Flutter
关注(0)|答案(1)|浏览(83)

我有一个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中定义)“无法分配给参数”

qfe3c7zg

qfe3c7zg1#

我创建了一个结果模型,其中的子模型是相同的,然后我使用它。所以希望它能解析json。

class NestedJsonResponse {
  bool? success;
  int? statusCode;
  dynamic error;
  NestedJsonData? data;

  NestedJsonResponse({
    this.success,
    this.statusCode,
    this.error,
    this.data,
  });

  factory NestedJsonResponse.fromJson(Map<String, dynamic> json) =>
      NestedJsonResponse(
        success: json["success"],
        statusCode: json["status_code"],
        error: json["error"],
        data: json["data"] == null ? null : NestedJsonData.fromJson(json["data"]),
      );

  Map<String, dynamic> toJson() => {
        "success": success,
        "status_code": statusCode,
        "error": error,
        "data": data?.toJson(),
      };
}

class NestedJsonData {
  String? message;
  int? hasPaginate;
  int? totalData;
  int? currentPage;
  int? from;
  int? to;
  String? firstPageUrl;
  int? lastPage;
  String? lastPageUrl;
  String? nextPageUrl;
  String? prevPageUrl;
  String? path;
  int? perPage;
  List<Result>? results;

  NestedJsonData({
    this.message,
    this.hasPaginate,
    this.totalData,
    this.currentPage,
    this.from,
    this.to,
    this.firstPageUrl,
    this.lastPage,
    this.lastPageUrl,
    this.nextPageUrl,
    this.prevPageUrl,
    this.path,
    this.perPage,
    this.results,
  });

  factory NestedJsonData.fromJson(Map<String, dynamic> json) => NestedJsonData(
        message: json["message"],
        hasPaginate: json["has_paginate"],
        totalData: json["total_data"],
        currentPage: json["current_page"],
        from: json["from"],
        to: json["to"],
        firstPageUrl: json["first_page_url"],
        lastPage: json["last_page"],
        lastPageUrl: json["last_page_url"],
        nextPageUrl: json["next_page_url"],
        prevPageUrl: json["prev_page_url"],
        path: json["path"],
        perPage: json["per_page"],
        results: json["results"] == null
            ? []
            : List<Result>.from(
                json["results"]!.map((x) => Result.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "message": message,
        "has_paginate": hasPaginate,
        "total_data": totalData,
        "current_page": currentPage,
        "from": from,
        "to": to,
        "first_page_url": firstPageUrl,
        "last_page": lastPage,
        "last_page_url": lastPageUrl,
        "next_page_url": nextPageUrl,
        "prev_page_url": prevPageUrl,
        "path": path,
        "per_page": perPage,
        "results": results == null
            ? []
            : List<dynamic>.from(results!.map((x) => x.toJson())),
      };
}

class Result {
  int? id;
  String? name;
  String? description;
  String? parentId;
  dynamic imagePath;
  String? isActive;
  dynamic deletedAt;
  DateTime? createdAt;
  DateTime? updatedAt;
  List<Result>? children;

  Result({
    this.id,
    this.name,
    this.description,
    this.parentId,
    this.imagePath,
    this.isActive,
    this.deletedAt,
    this.createdAt,
    this.updatedAt,
    this.children,
  });

  factory Result.fromJson(Map<String, dynamic> json) => Result(
        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"] == null
            ? null
            : DateTime.parse(json["created_at"]),
        updatedAt: json["updated_at"] == null
            ? null
            : DateTime.parse(json["updated_at"]),
        children: json["children"] == null
            ? []
            : List<Result>.from(
                json["children"]!.map((x) => Result.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "name": name,
        "description": description,
        "parent_id": parentId,
        "image_path": imagePath,
        "is_active": isActive,
        "deleted_at": deletedAt,
        "created_at": createdAt?.toIso8601String(),
        "updated_at": updatedAt?.toIso8601String(),
        "children": children == null
            ? []
            : List<dynamic>.from(children!.map((x) => x.toJson())),
      };
}

字符串

相关问题