Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You just need to make a condition like that:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading