fromJson static method

ChatMessage? fromJson(
  1. dynamic value
)

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

Implementation

// ignore: prefer_constructors_over_static_methods
static ChatMessage? 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 "ChatMessage[id]" is missing from JSON.');
      assert(json[r'id'] != null, 'Required key "ChatMessage[id]" has a null value in JSON.');
      assert(json.containsKey(r'channelId'), 'Required key "ChatMessage[channelId]" is missing from JSON.');
      assert(json[r'channelId'] != null, 'Required key "ChatMessage[channelId]" has a null value in JSON.');
      assert(json.containsKey(r'userId'), 'Required key "ChatMessage[userId]" is missing from JSON.');
      assert(json[r'userId'] != null, 'Required key "ChatMessage[userId]" has a null value in JSON.');
      assert(json.containsKey(r'text'), 'Required key "ChatMessage[text]" is missing from JSON.');
      assert(json[r'text'] != null, 'Required key "ChatMessage[text]" has a null value in JSON.');
      assert(json.containsKey(r'timestamp'), 'Required key "ChatMessage[timestamp]" is missing from JSON.');
      assert(json[r'timestamp'] != null, 'Required key "ChatMessage[timestamp]" has a null value in JSON.');
      return true;
    }());

    return ChatMessage(
      id: mapValueOfType<String>(json, r'id')!,
      provider: mapValueOfType<String>(json, r'provider'),
      accountId: mapValueOfType<String>(json, r'accountId'),
      channelId: mapValueOfType<String>(json, r'channelId')!,
      userId: mapValueOfType<String>(json, r'userId')!,
      text: mapValueOfType<String>(json, r'text')!,
      threadId: mapValueOfType<String>(json, r'threadId'),
      timestamp: mapDateTime(json, r'timestamp', r'')!,
      replyCount: mapValueOfType<int>(json, r'replyCount'),
      extra: mapCastOfType<String, Object>(json, r'extra') ?? const {},
    );
  }
  return null;
}