fromJson static method

ConversationMessage? fromJson(
  1. dynamic value
)

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

Implementation

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

    return ConversationMessage(
      id: mapValueOfType<String>(json, r'id')!,
      conversationId: mapValueOfType<String>(json, r'conversation_id')!,
      role: mapValueOfType<String>(json, r'role')!,
      content: mapValueOfType<String>(json, r'content'),
      metadata: mapCastOfType<String, Object>(json, r'metadata') ?? const {},
      createdAt: mapDateTime(json, r'created_at', r''),
    );
  }
  return null;
}