deleteMessage static method
Future<void>
deleteMessage(
- int messageId, {
- required dynamic onSuccess(
- BaseMessage message
- required dynamic onError(
- CometChatException excep
Deletes the message with specified messageId.
you get an object of the BaseMessage class, with the deletedAt field set with the timestamp of the time the message was deleted. Also, the deletedBy field is set. These two fields can be used to identify if the message is deleted while iterating through a list of messages.
Messages can be deleted only when logged-in user is the sender.
Migration Note: Migrated from platform channels to native Dart implementation. Behavior and signature remain identical for backward compatibility.
Android Reference: MessagesRequest.deleteMessage(int messageId)
method could throw PlatformException with error codes specifying the cause
Implementation
static Future<void> deleteMessage(int messageId,
{required Function(BaseMessage message)? onSuccess,
required Function(CometChatException excep)? onError}) async {
try {
// Get SDK instance
final sdk = SdkRegistry.getInstance();
// Call native Dart message repository (convert int to String)
final deletedMessage =
await sdk.messages.deleteMessage(messageId.toString());
// Call success callback with the deleted message from API response
if (onSuccess != null) onSuccess(deletedMessage);
} 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);
}
}