fromJson static method

Message? fromJson(
  1. dynamic value
)

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

Implementation

// ignore: prefer_constructors_over_static_methods
static Message? 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(() {
      requiredKeys.forEach((key) {
        assert(json.containsKey(key), 'Required key "Message[$key]" is missing from JSON.');
        assert(json[key] != null, 'Required key "Message[$key]" has a null value in JSON.');
      });
      return true;
    }());

    return Message(
      attachments: Attachment.listFromJson(json[r'attachments']),
      beforeMessageSendFailed: mapValueOfType<bool>(json, r'before_message_send_failed'),
      cid: mapValueOfType<String>(json, r'cid')!,
      command: mapValueOfType<String>(json, r'command'),
      createdAt: mapDateTime(json, r'created_at', r'')!,
      custom: mapCastOfType<String, Object>(json, r'custom')!,
      deletedAt: mapDateTime(json, r'deleted_at', r''),
      deletedReplyCount: mapValueOfType<int>(json, r'deleted_reply_count')!,
      html: mapValueOfType<String>(json, r'html')!,
      i18n: mapCastOfType<String, String>(json, r'i18n') ?? const {},
      id: mapValueOfType<String>(json, r'id')!,
      // MANUAL_EDIT: List to List<String>
      imageLabels: json[r'image_labels'] == null
          ? const {}
          : mapCastOfType<String, List<String>>(json, r'image_labels') ?? const {},
      latestReactions: Reaction.listFromJson(json[r'latest_reactions']),
      mentionedUsers: UserObject.listFromJson(json[r'mentioned_users']),
      messageTextUpdatedAt: mapDateTime(json, r'message_text_updated_at', r''),
      mml: mapValueOfType<String>(json, r'mml'),
      ownReactions: Reaction.listFromJson(json[r'own_reactions']),
      parentId: mapValueOfType<String>(json, r'parent_id'),
      pinExpires: mapDateTime(json, r'pin_expires', r''),
      pinned: mapValueOfType<bool>(json, r'pinned')!,
      pinnedAt: mapDateTime(json, r'pinned_at', r''),
      pinnedBy: UserObject.fromJson(json[r'pinned_by']),
      poll: Poll.fromJson(json[r'poll']),
      pollId: mapValueOfType<String>(json, r'poll_id'),
      quotedMessage: Message.fromJson(json[r'quoted_message']),
      quotedMessageId: mapValueOfType<String>(json, r'quoted_message_id'),
      reactionCounts: mapCastOfType<String, int>(json, r'reaction_counts')!,
      reactionGroups: ReactionGroupResponse.mapFromJson(json[r'reaction_groups']),
      reactionScores: mapCastOfType<String, int>(json, r'reaction_scores')!,
      replyCount: mapValueOfType<int>(json, r'reply_count')!,
      shadowed: mapValueOfType<bool>(json, r'shadowed')!,
      showInChannel: mapValueOfType<bool>(json, r'show_in_channel'),
      silent: mapValueOfType<bool>(json, r'silent')!,
      text: mapValueOfType<String>(json, r'text')!,
      threadParticipants: UserObject.listFromJson(json[r'thread_participants']),
      type: MessageTypeEnum.fromJson(json[r'type'])!,
      updatedAt: mapDateTime(json, r'updated_at', r'')!,
      user: UserObject.fromJson(json[r'user']),
    );
  }
  return null;
}