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