I want to handle dateTime null value in JSON parsing in flutter

Advertisements

I need help handling the null values for DateTime JSON parse. Since some of the data does not have createDate, I have a problem showing the data
this is my code:

factory OfaModel.fromJson(Map<String, dynamic> json) => TestModel(
    createdDate: DateTime.parse(json["CreatedDate"]),
    recordtypeBm: json["recordtype_bm"],
    amsCustodialHistoryBm: List<String>.from(json["AMSCustodialHistoryBM"].map((x) => x)),
    subjectCode: json["SubjectCode"]
    recordtypeBi: json["recordtype_bi"],
  );

>Solution :

You just need to make a condition like that:

json["CreatedDate"] == null ? null : DateTime.parse(json["CreatedDate"])

Leave a ReplyCancel reply