editMessage static method

dynamic editMessage(
  1. BaseMessage message, {
  2. required dynamic onSuccess(
    1. BaseMessage retMessage
    )?,
  3. required dynamic onError(
    1. CometChatException excep
    )?,
})

edit a message, you can use the editMessage() method

only allowed to edit TextMessage and CustomMessage

Migration Note: Migrated from platform channels to native Dart implementation. Behavior and signature remain identical for backward compatibility.

Android Reference: ApiConnection.java:editMessage() line 1213 — body is TextMessage.toMap() / CustomMessage.toMap() / MediaMessage.toMap()

method could throw PlatformException with error codes specifying the cause

Implementation

static editMessage(BaseMessage message,
    {required Function(BaseMessage retMessage)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    // Validate message type (only TextMessage and CustomMessage allowed)
    if (message.type != CometChatMessageType.text &&
        message.type != CometChatMessageType.custom) {
      throw CometChatException(
        ErrorCode.errorInvalidParameter,
        'Only text and custom messages can be edited',
        'Only text and custom messages can be edited',
      );
    }

    // Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // Call native Dart message repository with the full BaseMessage
    // The repository converts to the full message map via MessageMapper.toDto()
    final editedMessage = await sdk.messages.editMessage(message);

    // Call success callback
    if (onSuccess != null) onSuccess(editedMessage);
    return editedMessage;
  } on SdkException catch (sdkEx) {
    // Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
  }
}