fromJson static method

Draft? fromJson(
  1. dynamic value
)

Returns a new Draft instance and imports its values from value if it's a Map, null otherwise.

Implementation

// ignore: prefer_constructors_over_static_methods
static Draft? fromJson(dynamic value) {
  if (value is Map) {
    final json = value.cast<String, dynamic>();

    // Ensure that the map contains the required keys.
    // Note 1: the values aren't checked for validity beyond being non-null.
    // Note 2: this code is stripped in release mode!
    assert(() {
      assert(json.containsKey(r'id'), 'Required key "Draft[id]" is missing from JSON.');
      assert(json[r'id'] != null, 'Required key "Draft[id]" has a null value in JSON.');
      assert(json.containsKey(r'messageId'), 'Required key "Draft[messageId]" is missing from JSON.');
      assert(json[r'messageId'] != null, 'Required key "Draft[messageId]" has a null value in JSON.');
      assert(json.containsKey(r'html'), 'Required key "Draft[html]" is missing from JSON.');
      assert(json[r'html'] != null, 'Required key "Draft[html]" has a null value in JSON.');
      assert(json.containsKey(r'createdAt'), 'Required key "Draft[createdAt]" is missing from JSON.');
      assert(json[r'createdAt'] != null, 'Required key "Draft[createdAt]" has a null value in JSON.');
      assert(json.containsKey(r'updatedAt'), 'Required key "Draft[updatedAt]" is missing from JSON.');
      assert(json[r'updatedAt'] != null, 'Required key "Draft[updatedAt]" has a null value in JSON.');
      return true;
    }());

    return Draft(
      id: mapValueOfType<String>(json, r'id')!,
      messageId: mapValueOfType<String>(json, r'messageId')!,
      threadId: mapValueOfType<String>(json, r'threadId'),
      to: json[r'to'] is Iterable
          ? (json[r'to'] as Iterable).cast<String>().toList(growable: false)
          : const [],
      cc: json[r'cc'] is Iterable
          ? (json[r'cc'] as Iterable).cast<String>().toList(growable: false)
          : const [],
      bcc: json[r'bcc'] is Iterable
          ? (json[r'bcc'] as Iterable).cast<String>().toList(growable: false)
          : const [],
      subject: mapValueOfType<String>(json, r'subject'),
      body: mapValueOfType<String>(json, r'body'),
      html: mapValueOfType<bool>(json, r'html')!,
      attachments: AttachmentMeta.listFromJson(json[r'attachments']),
      createdAt: mapDateTime(json, r'createdAt', r'')!,
      updatedAt: mapDateTime(json, r'updatedAt', r'')!,
    );
  }
  return null;
}