简体   繁体   中英

JSON to Dart conversion errors: doesn't print

I'm trying to get a data from a JSON Response. Here's my sample JSON data:

{
            "id": 97,
            "parentWo": "2022-178348",
            "parentWoDesc": "COMMERCIAL - BUSINESS ESTABLISHMENT - LUMBIA HOSPITAL - PN 21.0130",
            "parentWoStatus": "APPR",
            "parentWoStatusDate": "2022-03-22T13:36:00+08:00",
            "parentWoWorkType": "CP",
            "parentWoClass": "COMM",
            "parentWoCrew": "",
            "parentWoLead": "",
            "parentWoLocation": "210130",
            "parentWoDepartmentDescription": "Line Maintenance Department",
            "parentWoSectionDescription": "Line Section",
            "status": "I",
            "approvedBy": "eams.extension2",
            "approvedDate": "2022-07-21T09:59:09.727428+08:00",
            "disapprovedBy": null,
            "disapprovedDate": null,
            "issuedBy": "eams.extension2",
            "issuedDate": "2022-07-21T11:19:39.717066+08:00",
            "voidedBy": null,
            "voidedDate": null,
            "receivedBy": "ALEX",
            "receivedDate": "2022-07-21T11:19:39.717073+08:00",
            "children": [
                {
                    "id": 27,
                    "woNum": "2022-178349",
                    "woDesc": "Installation of POLE, CON, SPUN TY, (45) 14M X 500 [Rocky]",
                    "woStatus": "APPR",
                    "woStatusDate": "2022-03-22T13:36:01+08:00",
                    "woCrew": "",
                    "woLead": "",
                    "woLocation": "212636",
                    "materials": [
                        {
                            "id": 36,
                            "itemNum": "2001409",
                            "itemDescription": "POLE, CON, SPUN TY, (45) 14M X 500",
                            "itemQuantityPlan": 1.0,
                            "itemQuantityIssued": 1.0,
                            "itemWoBalance": 0.0,
                            "itemInventoryBalance": -1669.0,
                            "itemCommodity": "",
                            "itemIssueUnit": "LEN",
                            "itemStructure": false,
                            "itemDtPole": true,
                            "requestQuantity": "10.00",
                            "approvedQuantity": "8.00",
                            "issuedQuantity": "5.00",
                            "timestamp": "2022-07-20T11:21:37.607303+08:00",
                            "updated": "2022-07-21T11:19:39.730245+08:00"
                        }
                    ]
                }
 
                
            ]
            }

Then here's my Class for the JSON above:

class MROParent {

  int? id;
    String? parentWo;
    String? parentWoDesc;
    String? parentWoStatus;
    DateTime? parentWoStatusDate;
    String? parentWoWorkType;
    String? parentWoClass;
    String? parentWoCrew;
    String? parentWoLead;
    String? parentWoLocation;
    String? parentWoDepartmentDescription;
    String? parentWoSectionDescription;
    String? status;
    String? approvedBy;
    DateTime? approvedDate;
    String? disapprovedBy;
    DateTime? disapprovedDate;
    String? issuedBy;
    DateTime? issuedDate;
    String? voidedBy;
    DateTime? voidedDate;
    String? receivedBy;
    DateTime? receivedDate;
    List<MROChild>? children;

  MROParent({
    this.id,
        this.parentWo,
        this.parentWoDesc,
        this.parentWoStatus,
        this.parentWoStatusDate,
        this.parentWoWorkType,
        this.parentWoClass,
        this.parentWoCrew,
        this.parentWoLead,
        this.parentWoLocation,
        this.parentWoDepartmentDescription,
        this.parentWoSectionDescription,
        this.status,
        this.approvedBy,
        this.approvedDate,
        this.disapprovedBy,
        this.disapprovedDate,
        this.issuedBy,
        this.issuedDate,
        this.voidedBy,
        this.voidedDate,
        this.receivedBy,
        this.receivedDate,
        this.children,
  });

  factory MROParent.fromJson(Map<String, dynamic> json) {
    var list = json['children'] as List;
    print(list.runtimeType);
    List<MROChild> childrenlist = list.map((i) => MROChild.fromJson(i)).toList();

    return MROParent(
      id: json["id"],
        parentWo: json["parentWo"],
        parentWoDesc: json["parentWoDesc"],
        parentWoStatus: json["parentWoStatus"],
        parentWoStatusDate: DateTime.parse(json["parentWoStatusDate"]),
        parentWoWorkType: json["parentWoWorkType"],
        parentWoClass: json["parentWoClass"],
        parentWoCrew: json["parentWoCrew"],
        parentWoLead: json["parentWoLead"],
        parentWoLocation: json["parentWoLocation"],
        parentWoDepartmentDescription: json["parentWoDepartmentDescription"],
        parentWoSectionDescription: json["parentWoSectionDescription"],
        status: json["status"],
        approvedBy: json["approvedBy"],
        approvedDate: DateTime.parse(json["approvedDate"]),
        disapprovedBy: json["disapprovedBy"],
        disapprovedDate: DateTime.parse(json["disapprovedDate"]),
        issuedBy: json["issuedBy"],
        issuedDate: DateTime.parse(json["issuedDate"]),
        voidedBy: json["voidedBy"],
        voidedDate: DateTime.parse(json["voidedDate"]),
        receivedBy: json["receivedBy"],
        receivedDate: DateTime.parse(json["receivedDate"]),
      children:
          childrenlist,
    );
  }
  

  Map<String, dynamic> toJson() => {
        "id": id,
        "parentWo": parentWo,
        "parentWoDesc": parentWoDesc,
        "parentWoStatus": parentWoStatus,
        "parentWoStatusDate": parentWoStatusDate!.toIso8601String(),
        "parentWoWorkType": parentWoWorkType,
        "parentWoClass": parentWoClass,
        "parentWoCrew": parentWoCrew,
        "parentWoLead": parentWoLead,
        "parentWoLocation": parentWoLocation,
        "parentWoDepartmentDescription": parentWoDepartmentDescription,
        "parentWoSectionDescription": parentWoSectionDescription,
        "status": status,
        "approvedBy": approvedBy,
        "approvedDate": approvedDate!.toIso8601String(),
        "disapprovedBy": disapprovedBy,
        "disapprovedDate": disapprovedDate!.toIso8601String(),
        "issuedBy": issuedBy,
        "issuedDate": issuedDate!.toIso8601String(),
        "voidedBy": voidedBy,
        "voidedDate": voidedDate!.toIso8601String(),
        "receivedBy": receivedBy,
        "receivedDate": receivedDate!.toIso8601String(),
        "children": List<dynamic>.from(children!.map((x) => x.toJson())),
      };
}

class MROChild {

  int? id;
    String? woNum;
    String? woDesc;
    String? woStatus;
    DateTime? woStatusDate;
    String? woCrew;
    String? woLead;
    String? woLocation;
    List<MROMaterial>? materials;

  MROChild({
    this.id,
        this.woNum,
        this.woDesc,
        this.woStatus,
        this.woStatusDate,
        this.woCrew,
        this.woLead,
        this.woLocation,
        this.materials,
  });

  factory MROChild.fromJson(Map<String, dynamic> json) {
    var list = json['materials'] as List;
    print(list.runtimeType);
    List<MROMaterial> materialslist = list.map((i) => MROMaterial.fromJson(i)).toList();

    return MROChild(
        id: json["id"],
        woNum: json["woNum"],
        woDesc: json["woDesc"],
        woStatus: json["woStatus"],
        woStatusDate: DateTime.parse(json["woStatusDate"]),
        woCrew: json["woCrew"],
        woLead: json["woLead"],
        woLocation: json["woLocation"],
        materials: materialslist,
      );
  } 

  Map<String, dynamic> toJson() => {
        "id": id,
        "woNum": woNum,
        "woDesc": woDesc,
        "woStatus": woStatus,
        "woStatusDate": woStatusDate!.toIso8601String(),
        "woCrew": woCrew,
        "woLead": woLead,
        "woLocation": woLocation,
        "materials": List<dynamic>.from(materials!.map((x) => x.toJson())),
      };
  get length => null;
}

class MROMaterial {

  int? id;
    String? itemNum;
    String? itemDescription;
    double? itemQuantityPlan;
    double? itemQuantityIssued;
    double? itemWoBalance;
    double? itemInventoryBalance;
    String? itemCommodity;
    String? itemIssueUnit;
    bool? itemStructure;
    bool? itemDtPole;
    double? requestQuantity;
    double? approvedQuantity;
    double? issuedQuantity;
    DateTime? timestamp;
    DateTime? updated;
  
  MROMaterial({
    this.id,
        this.itemNum,
        this.itemDescription,
        this.itemQuantityPlan,
        this.itemQuantityIssued,
        this.itemWoBalance,
        this.itemInventoryBalance,
        this.itemCommodity,
        this.itemIssueUnit,
        this.itemStructure,
        this.itemDtPole,
        this.requestQuantity,
        this.approvedQuantity,
        this.issuedQuantity,
        this.timestamp,
        this.updated,
  });

  factory MROMaterial.fromJson(Map<String, dynamic> json) => MROMaterial(
        id: json["id"],
        itemNum: json["itemNum"],
        itemDescription: json["itemDescription"],
        itemQuantityPlan: json["itemQuantityPlan"],
        itemQuantityIssued: json["itemQuantityIssued"],
        itemWoBalance: json["itemWoBalance"],
        itemInventoryBalance: json["itemInventoryBalance"],
        itemCommodity: json["itemCommodity"],
        itemIssueUnit: json["itemIssueUnit"],
        itemStructure: json["itemStructure"],
        itemDtPole: json["itemDtPole"],
        requestQuantity: json["requestQuantity"],
        approvedQuantity: json["approvedQuantity"],
        issuedQuantity: json["issuedQuantity"],
        timestamp: DateTime.parse(json["timestamp"]),
        updated: DateTime.parse(json["updated"]),
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "itemNum": itemNum,
        "itemDescription": itemDescription,
        "itemQuantityPlan": itemQuantityPlan,
        "itemQuantityIssued": itemQuantityIssued,
        "itemWoBalance": itemWoBalance,
        "itemInventoryBalance": itemInventoryBalance,
        "itemCommodity": itemCommodity,
        "itemIssueUnit": itemIssueUnit,
        "itemStructure": itemStructure,
        "itemDtPole": itemDtPole,
        "requestQuantity": requestQuantity,
        "approvedQuantity": approvedQuantity,
        "issuedQuantity": issuedQuantity,
        "timestamp": timestamp!.toIso8601String(),
        "updated": updated!.toIso8601String(),
      };
  @override
  String toString() {
    // TODO: implement toString
    return '$id';
  }
}

The code below doesn't seem to work when I try to save it as a new class, and try to print a value from it:

      print('THIS HAVE RESULTS IN MRO PARENT'); //this one prints
      final parentMROResponse = jsonResponse['results']['data'];
      print(parentMROResponse); //this one prints

      mainParentMROO = MROParent.fromJson(parentMROResponse);
      print(mainParentMROO.id); //THIS IS DOESN'T PRINT

Is there something I'm missing? I tried checking the data types and I think all are correct. thanks!

Change int to dynamic your problem will be solved !

class MROMaterial {
    
      dynamic id;
        String? itemNum;
        String? itemDescription;
        dynamic itemQuantityPlan;
        dynamic itemQuantityIssued;
        dynamic itemWoBalance;
        dynamic itemInventoryBalance;
        String? itemCommodity;
        String? itemIssueUnit;
        bool? itemStructure;
        bool? itemDtPole;
        dynamic requestQuantity;
        dynamic approvedQuantity;
        dynamic issuedQuantity;
        DateTime? timestamp;
        DateTime? updated;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM