> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Edit A Message

> Edit CometChat text and custom messages in Flutter apps and handle real-time or missed edit events.

> [accordion] AI Integration Quick Reference
>
> | Field | Value |
> | - | - |
> | Package | `cometchat_sdk` |
> | Import | `import 'package:cometchat_sdk/cometchat_sdk.dart';` |
> | Purpose | Edit CometChat text and custom messages in Flutter apps and handle real-time or missed edit events. |
> | Key methods | `addMessageListener()` · `editMessage()` |
> | Key classes | `BaseMessage` · `TextMessage` · `CometChatException` |
> | Listener callbacks | `onMessageEdited()` |
> | Prerequisites | SDK initialised via [`CometChat.init()`](https://www.cometchat.com/docs/sdk/flutter/setup) and a logged-in user via [`CometChat.login()`](https://www.cometchat.com/docs/sdk/flutter/authentication-overview). |
> | Constraints | `onSuccess` and `onError` are **both required** — omitting either is a compile error, and awaiting the call alone gives you nothing to act on. Put the success path inside `onSuccess`. |
> | Related | [Send A Message](https://www.cometchat.com/docs/sdk/flutter/send-message.md) · [Delete A Message](https://www.cometchat.com/docs/sdk/flutter/delete-message.md) |
> | Full reference | [`BaseMessage`](https://www.cometchat.com/docs/sdk/reference/messages#basemessage) · [`TextMessage`](https://www.cometchat.com/docs/sdk/reference/messages#textmessage) · [`CometChatException`](https://www.cometchat.com/docs/sdk/reference/auxiliary#cometchatexception) |

While editing a message is straightforward, receiving events for edited messages with CometChat has two parts:

1. Adding a listener to receive [real-time message edit events](https://www.cometchat.com/docs/sdk/flutter/edit-message.md#real-time-message-edit-events) when your app is running
2. Calling a method to retrieve [missed message edit events](https://www.cometchat.com/docs/sdk/flutter/edit-message.md#missed-message-edit-events) when your app was not running

## Edit a Message

*In other words, as a sender, how do I edit a message?*

In order to edit a message, you can use the `editMessage()` method. This method takes an object of the `BaseMessage` class. At the moment, you are only allowed to edit `TextMessage` and `CustomMessage`. Thus, the `BaseMessage` object must either be a Text or a Custom Message.

> [!NOTE]
>
> Editing a message needs a connection — with offline support enabled, `editMessage()` is **not** queued for later. After `onSuccess`, the edit is applied to the message in the on-device cache if it is already cached; it never adds a new message. See [Send While Offline](https://www.cometchat.com/docs/sdk/flutter/offline-send.md).

### Add/Update Tags

While editing a message, you can update the tags associated with the Message. You can use the `setTags()` method to do so. The tags added while editing a message will replace the tags set when the message was sent.

> [tab] Dart
>
> ```dart theme={null}
> List<String> tags = [];
> tags.add("pinned");
> textMessage.tags = tags; 
> ```

Once the message object is ready, you can use the `editMessage()` method and pass the message object to it.

> [tab] Dart
>
> ```dart theme={null}
> TextMessage updatedMessage = TextMessage(
>                         text: updatedText,
>                         receiverUid: receiverID,
>                         receiverType: receiverType,
>                         type: type);
>
> updatedMessage.id = message.id;
>
> await CometChat.editMessage(updatedMessage, 
>   onSuccess: (BaseMessage message){
>   	debugPrint("Message Edited successfully: $message");
>   }, onError: (CometChatException e){
>   	debugPrint("Message Edited failed with exception: ${e.message}");
>   }
> );
> ```

The object of the edited message will be returned in the `onSuccess()`callback method of the listener. The message object will contain the `editedAt` field set with the timestamp of the time the message was edited. This will help you identify if the message was edited while iterating through the list of messages. The `editedBy` field is also set to the `UID` of the user who edited the message.

By default, CometChat allows certain roles to edit a message.

| User Role | Conversation Type | Edit Capabilities |
| - | - | - |
| Message Sender | One-on-one Conversation | Messages they've sent |
| Message Sender | Group Conversation | Messages they've sent |
| Group Owner | Group Conversation | All messages in the group |
| Group Moderator | Group Conversation | All messages in the group |

## Real-time Message Edit Events

*In other words, as a recipient, how do I know when someone has edited their message when my app is running?*

In order to receive real-time events for message being edited, you need to override the `onMessageEdited()` method of the `MessageListener` class.

> [tab] Dart
>
> ```dart theme={null}
> class Class_Name  with MessageListener {
>
> //CometChat.addMessageListener("listenerId", this);
> @override
> void onMessageEdited(BaseMessage message) {
>   // TODO: implement onMessageEdited
> }
>
>
> } 
> ```

## Missed Message Edit Events

*In other words, as a recipient, how do I know when someone edited their message when my app was not running?*

When you retrieve the list of previous messages, for the message that was edited, the `editedAt` and the `editedBy` fields will be set. Also, for example, if the total number of messages for a conversation is 100, and the message with message ID 50 was edited. Now the message with ID 50 will have the `editedAt` and the `editedBy` fields set whenever it is pulled from the history. Also, the 101st message will be an `Action` message informing you that the message with ID 50 has been edited..

For the message edited event, in the `Action` object received, the following fields can help you get the relevant information-

1. `action` - `edited`
2. `actionOn` - Updated message object with the edited details.
3. `actionBy` - User object containing the details of the user who has edited the message.
4. `actionFor` - User/group object having the details of the receiver to which the message was sent.

> [!NOTE]
>
> In order to edit a message, you need to be either the sender of the message or the admin/moderator of the group in which the message was sent.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.