Model.fromJson constructor

Model.fromJson(
  1. Map<String, dynamic> json
)

Constructs model from JSON.

Parses created_at and updated_at safely.

Implementation

Model.fromJson(Map<String, dynamic> json) {
  id = json['id'] as int?;

  uuid = json['uuid'] as String?;

  if (json['created_at'] != null) {
    //  try here for DateTime.parse
    try {
      createdAt = DateTime.parse(json['created_at']);
    } catch (e) {
      // Todo - more tests needed here to decide on created_at/updated_at formats
      // Postgres returns a string which doesnt parse to date
      // a string wont have datetime props and methods.
      // considering a get createdAt =>
      // first test seem to be passing... leaving for redundancy
      // key your eye on datetime format behaviors from different dbs
      //datetime might not be able to parse it!
      createdAt = json['created_at'];
    }
  }

  if (json['updated_at'] != null) {
    //  try here for DateTime.parse
    try {
      // updatedAt = json['updated_at'];
      updatedAt = DateTime.parse(json['updated_at']);
    } catch (e) {
      // Todo - more tests needed here to decide on created_at/updated_at formats
      // Postgres returns a string which doesnt parse to date
      updatedAt = json['updated_at'];
    }
  }
}