class Part { int id; int reportPartID; String partNo; String partName; int quantity; double returnQty, installQty; Part({ this.id, this.reportPartID, this.partNo, this.partName, this.quantity = 1, this.installQty, this.returnQty, }); Map toJson() { return { "id": reportPartID ?? 0, "sparePart": {"id": id, "partNo": partNo, "partName": partName}, "qty": quantity, if (returnQty != null) "returnQty": returnQty, if (installQty != null) "installQty": installQty, }; } factory Part.fromJson(Map parsedJson, {Map reportJson}) { return Part( id: parsedJson["id"], reportPartID: reportJson != null ? reportJson["id"] : null, partNo: parsedJson["partNo"], partName: parsedJson["partName"], quantity: reportJson != null ? (reportJson["qty"] ?? 1).toInt() : 1, returnQty: parsedJson["returnQty"], installQty: parsedJson["installQty"], ); } }