fromJson static method

Email? fromJson(
  1. dynamic value
)

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

Implementation

// ignore: prefer_constructors_over_static_methods
static Email? 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 "Email[id]" is missing from JSON.');
      assert(json[r'id'] != null, 'Required key "Email[id]" has a null value in JSON.');
      assert(json.containsKey(r'subject'), 'Required key "Email[subject]" is missing from JSON.');
      assert(json[r'subject'] != null, 'Required key "Email[subject]" has a null value in JSON.');
      assert(json.containsKey(r'from'), 'Required key "Email[from]" is missing from JSON.');
      assert(json[r'from'] != null, 'Required key "Email[from]" has a null value in JSON.');
      assert(json.containsKey(r'to'), 'Required key "Email[to]" is missing from JSON.');
      assert(json[r'to'] != null, 'Required key "Email[to]" has a null value in JSON.');
      assert(json.containsKey(r'body'), 'Required key "Email[body]" is missing from JSON.');
      assert(json[r'body'] != null, 'Required key "Email[body]" has a null value in JSON.');
      assert(json.containsKey(r'html'), 'Required key "Email[html]" is missing from JSON.');
      assert(json[r'html'] != null, 'Required key "Email[html]" has a null value in JSON.');
      assert(json.containsKey(r'date'), 'Required key "Email[date]" is missing from JSON.');
      assert(json[r'date'] != null, 'Required key "Email[date]" has a null value in JSON.');
      assert(json.containsKey(r'isRead'), 'Required key "Email[isRead]" is missing from JSON.');
      assert(json[r'isRead'] != null, 'Required key "Email[isRead]" has a null value in JSON.');
      assert(json.containsKey(r'isStarred'), 'Required key "Email[isStarred]" is missing from JSON.');
      assert(json[r'isStarred'] != null, 'Required key "Email[isStarred]" has a null value in JSON.');
      return true;
    }());

    return Email(
      id: mapValueOfType<String>(json, r'id')!,
      threadId: mapValueOfType<String>(json, r'threadId'),
      provider: mapValueOfType<String>(json, r'provider'),
      accountId: mapValueOfType<String>(json, r'accountId'),
      subject: mapValueOfType<String>(json, r'subject')!,
      from: mapValueOfType<String>(json, r'from')!,
      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 [],
      body: mapValueOfType<String>(json, r'body')!,
      html: mapValueOfType<bool>(json, r'html')!,
      date: mapDateTime(json, r'date', r'')!,
      labels: json[r'labels'] is Iterable
          ? (json[r'labels'] as Iterable).cast<String>().toList(growable: false)
          : const [],
      isRead: mapValueOfType<bool>(json, r'isRead')!,
      isStarred: mapValueOfType<bool>(json, r'isStarred')!,
      attachments: AttachmentMeta.listFromJson(json[r'attachments']),
      snippet: mapValueOfType<String>(json, r'snippet'),
      messageId: mapValueOfType<String>(json, r'messageId'),
      inReplyTo: mapValueOfType<String>(json, r'inReplyTo'),
      references: json[r'references'] is Iterable
          ? (json[r'references'] as Iterable).cast<String>().toList(growable: false)
          : const [],
    );
  }
  return null;
}