0

i create flutter app which use the api from server when i get the JSON response from the server I parse the response and retrieve the sicke, serviceName, date, user, status and some more information about sicks, The problem is that some sicks in the JSON response don't have the doctororder key or medicalImagingOrder key and has diffrent key . When I try to get those JSON key values the program throws an error how i should handel this in the flutter?

this is how i create my object

class Datum {
  Datum({
    this.id,
    this.sicke,
    this.serviceName,
    this.doctorOrder,
    this.date,
    this.user,
    this.statusNumber,
    this.status,
    this.statusHistory,
    this.insertDate,
    this.statuses,
    this.excessInvoice,
    this.price,
    this.shippingCosts,
    this.vat,
    this.netPrice,
    this.totalPrice,
    this.orderNumber,
    this.v,
    this.cancleReason,
    this.medicalImagingOrder,
    this.laboratoryOrder,
    this.serviceProvider,
    this.nurseOrder,
  });

  String id;
  Sicke sicke;
  String serviceName;
  DoctorOrder doctorOrder;
  DateTime date;
  User user;
  int statusNumber;
  StatusEnum status;
  List<StatusHistory> statusHistory;
  DateTime insertDate;
  List<StatusElement> statuses;
  List<dynamic> excessInvoice;
  int price;
  int shippingCosts;
  int vat;
  int netPrice;
  int totalPrice;
  int orderNumber;
  int v;
  String cancleReason;
  Order medicalImagingOrder;
  LaboratoryOrder laboratoryOrder;
  String serviceProvider;
  Order nurseOrder;

  factory Datum.fromJson(Map<String, dynamic> json) => Datum(
    id: json["_id"],
    sicke: json["sicke"] == null ? null : Sicke.fromJson(json["sicke"]),
    serviceName: json["serviceName"],
    doctorOrder: json["doctorOrder"] == null ? null : DoctorOrder.fromJson(json["doctorOrder"]),
    date: DateTime.parse(json["date"]),
    user: userValues.map[json["user"]],
    statusNumber: json["statusNumber"],
    status: statusEnumValues.map[json["status"]],
    statusHistory: List<StatusHistory>.from(json["statusHistory"].map((x) => StatusHistory.fromJson(x))),
    insertDate: DateTime.parse(json["insertDate"]),
    statuses: List<StatusElement>.from(json["statuses"].map((x) => StatusElement.fromJson(x))),
    excessInvoice: List<dynamic>.from(json["excessInvoice"].map((x) => x)),
    price: json["price"],
    shippingCosts: json["shippingCosts"],
    vat: json["VAT"],
    netPrice: json["netPrice"],
    totalPrice: json["totalPrice"],
    orderNumber: json["orderNumber"],
    v: json["__v"],
    cancleReason: json["cancleReason"] == null ? null : json["cancleReason"],
    medicalImagingOrder: json["medicalImagingOrder"] == null ? null : Order.fromJson(json["medicalImagingOrder"]),
    laboratoryOrder: json["laboratoryOrder"] == null ? null : LaboratoryOrder.fromJson(json["laboratoryOrder"]),
    serviceProvider: json["serviceProvider"] == null ? null : json["serviceProvider"],
    nurseOrder: json["nurseOrder"] == null ? null : Order.fromJson(json["nurseOrder"]),
  );

and this is my json response enter image description here

Masoud H
  • 194
  • 4
  • 15

1 Answers1

0

You have a list of Datum in your json response. So you can start with :

List<Datum> datumFromJson(String str) => List<Datum>.from(json.decode(str).map((x) => Datum.fromJson(x)));
String datumToJson(List<Datum> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

After that you can start the class:

class Datum {
Datum({
    this.id,
    this.sicke,
    this.serviceName,
    this.doctorOrder,
    this.date,
    this.user,
    this.statusNumber,
    this.status,
    this.statusHistory,
    this.insertDate,
    this.statuses,
    this.excessInvoice,
    this.price,
    this.shippingCosts,
    this.vat,
    this.netPrice,
    this.totalPrice,
    this.orderNumber,
    this.v,
    this.cancleReason,
    this.medicalImagingOrder,
    this.laboratoryOrder,
    this.serviceProvider,
    this.nurseOrder,
});

String id;
LaboratoryOrder sicke;
String serviceName;
String doctorOrder;
String date;
LaboratoryOrder user;
int statusNumber;
LaboratoryOrder status;
List<LaboratoryOrder> statusHistory;
String insertDate;
List<LaboratoryOrder> statuses;
List<LaboratoryOrder> excessInvoice;
int price;
int shippingCosts;
int vat;
int netPrice;
int totalPrice;
int orderNumber;
int v;
String cancleReason;
LaboratoryOrder medicalImagingOrder;
LaboratoryOrder laboratoryOrder;
String serviceProvider;
LaboratoryOrder nurseOrder;

factory Datum.fromJson(Map<String, dynamic> json) => Datum(
    id: json["id"],
    sicke: LaboratoryOrder.fromJson(json["sicke"]),
    serviceName: json["serviceName"],
    doctorOrder: json["doctorOrder"],
    date: json["date"],
    user: LaboratoryOrder.fromJson(json["user"]),
    statusNumber: json["statusNumber"],
    status: LaboratoryOrder.fromJson(json["status"]),
    statusHistory: List<LaboratoryOrder>.from(json["statusHistory"].map((x) => LaboratoryOrder.fromJson(x))),
    insertDate: json["insertDate"],
    statuses: List<LaboratoryOrder>.from(json["statuses"].map((x) => LaboratoryOrder.fromJson(x))),
    excessInvoice: List<LaboratoryOrder>.from(json["excessInvoice"].map((x) => LaboratoryOrder.fromJson(x))),
    price: json["price"],
    shippingCosts: json["shippingCosts"],
    vat: json["vat"],
    netPrice: json["netPrice"],
    totalPrice: json["totalPrice"],
    orderNumber: json["orderNumber"],
    v: json["v"],
    cancleReason: json["cancleReason"],
    medicalImagingOrder: LaboratoryOrder.fromJson(json["medicalImagingOrder"]),
    laboratoryOrder: LaboratoryOrder.fromJson(json["laboratoryOrder"]),
    serviceProvider: json["serviceProvider"],
    nurseOrder: LaboratoryOrder.fromJson(json["nurseOrder"]),
);

Map<String, dynamic> toJson() => {
    "id": id,
    "sicke": sicke.toJson(),
    "serviceName": serviceName,
    "doctorOrder": doctorOrder,
    "date": date,
    "user": user.toJson(),
    "statusNumber": statusNumber,
    "status": status.toJson(),
    "statusHistory": List<dynamic>.from(statusHistory.map((x) => x.toJson())),
    "insertDate": insertDate,
    "statuses": List<dynamic>.from(statuses.map((x) => x.toJson())),
    "excessInvoice": List<dynamic>.from(excessInvoice.map((x) => x.toJson())),
    "price": price,
    "shippingCosts": shippingCosts,
    "vat": vat,
    "netPrice": netPrice,
    "totalPrice": totalPrice,
    "orderNumber": orderNumber,
    "v": v,
    "cancleReason": cancleReason,
    "medicalImagingOrder": medicalImagingOrder.toJson(),
    "laboratoryOrder": laboratoryOrder.toJson(),
    "serviceProvider": serviceProvider,
    "nurseOrder": nurseOrder.toJson(),
};}

And finaly a class of your specific objects:

class LaboratoryOrder {
LaboratoryOrder();

factory LaboratoryOrder.fromJson(Map<String, dynamic> json) => LaboratoryOrder(
);
Kayson
  • 28
  • 1
  • 7