editMessage static method
Future<BaseMessage?>
editMessage(
- BaseMessage message, {
- required dynamic onSuccess()?,
- required dynamic onError(
- CometChatException excep
edit a message, you can use the editMessage() method
allowed to edit TextMessage, CustomMessage, and MediaMessage (caption only)
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 Future<BaseMessage?> editMessage(BaseMessage message,
{required Function(BaseMessage retMessage)? onSuccess,
required Function(CometChatException excep)? onError}) async {
try {
// Validate message type (Text, Custom, and Media — the mapper/repository
// already serialize all three; Media edits only ever change the caption).
const editableTypes = {
CometChatMessageType.text,
CometChatMessageType.custom,
CometChatMessageType.image,
CometChatMessageType.video,
CometChatMessageType.audio,
CometChatMessageType.file,
};
if (!editableTypes.contains(message.type)) {
throw CometChatException(
ErrorCode.errorInvalidParameter,
'Only text, custom, and media messages can be edited',
'Only text, custom, and media 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);
}
return null;
}