> ## Documentation Index
> Fetch the complete documentation index at: https://cometchat-22654f5b-docs-agent-in-group-react-v6.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Edit Message

> Edit text and custom messages using the CometChat Flutter SDK.

<Accordion title="AI Integration Quick Reference">
  | Field           | Value                                         |
  | --------------- | --------------------------------------------- |
  | Key Classes     | `TextMessage`, `CustomMessage`, `BaseMessage` |
  | Key Methods     | `CometChat.editMessage()`                     |
  | Listener Events | `onMessageEdited`                             |
  | Prerequisites   | SDK initialized, user logged in               |

  ```dart theme={null}
  // Edit a text message
  TextMessage updatedMessage = TextMessage(
    text: "Updated message text",
    receiverUid: "receiver_uid",
    receiverType: CometChatReceiverType.user,
    type: CometChatMessageType.text,
  );
  updatedMessage.id = originalMessageId;

  await CometChat.editMessage(updatedMessage,
    onSuccess: (BaseMessage message) {
      debugPrint("Message edited: $message");
    },
    onError: (CometChatException e) {
      debugPrint("Error: ${e.message}");
    },
  );

  // Listen for real-time edits
  CometChat.addMessageListener("edits_listener", MessageListener(
    onMessageEdited: (BaseMessage message) {
      debugPrint("Message was edited: ${message.id}");
    },
  ));

  // Remove listener when done
  CometChat.removeMessageListener("edits_listener");
  ```
</Accordion>

Editing a message is straightforward. Receiving edit events has two parts:

1. Adding a listener for [real-time edits](#real-time-message-edit-events) when your app is running
2. Fetching [missed edits](#missed-message-edit-events) when your app was offline

## Edit a Message

Use `editMessage()` with a [`TextMessage`](/sdk/reference/messages#textmessage) or [`CustomMessage`](/sdk/reference/messages#custommessage) object. Set the message ID using the `id` property.

| Parameter   | Type                           | Description                                                                                                                     |
| ----------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------- |
| `message`   | `BaseMessage`                  | The message object to edit. Must be a `TextMessage` or `CustomMessage`. Set the `id` property to the ID of the message to edit. |
| `onSuccess` | `Function(BaseMessage)`        | Callback triggered on success with the edited message object.                                                                   |
| `onError`   | `Function(CometChatException)` | Callback triggered on error with exception details.                                                                             |

### Add/Update Tags

Use `tags` to update tags when editing. New tags replace existing ones.

<Tabs>
  <Tab title="Text Message">
    ```dart theme={null}
    List<String> tags = [];
    tags.add("pinned");
    textMessage.tags = tags; 
    ```
  </Tab>

  <Tab title="Custom Message">
    ```dart theme={null}
    List<String> tags = [];
    tags.add("pinned");
    customMessage.tags = tags;
    ```
  </Tab>
</Tabs>

Once the message object is ready, call `editMessage()`.

<Tabs>
  <Tab title="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}");
      }
    );
    ```
  </Tab>
</Tabs>

<Accordion title="Response">
  **On Success** — A `BaseMessage` object containing all details of the edited message:

  <span id="edit-message-object" style={{scrollMarginTop: '100px'}} />

  **BaseMessage Object:**

  | Parameter            | Type   | Description                                        | Sample Value                                 |
  | -------------------- | ------ | -------------------------------------------------- | -------------------------------------------- |
  | `id`                 | number | Unique message ID                                  | `401`                                        |
  | `metadata`           | object | Custom metadata attached to the message            | `{}`                                         |
  | `receiver`           | object | Receiver user object                               | [See below ↓](#edit-message-receiver-object) |
  | `editedBy`           | string | UID of the user who edited the message             | `"cometchat-uid-1"`                          |
  | `conversationId`     | string | Unique conversation identifier                     | `"cometchat-uid-1_user_cometchat-uid-2"`     |
  | `sentAt`             | number | Epoch timestamp when the message was sent          | `1745554729`                                 |
  | `receiverUid`        | string | UID of the receiver                                | `"cometchat-uid-2"`                          |
  | `type`               | string | Type of the message                                | `"text"`                                     |
  | `readAt`             | number | Epoch timestamp when the message was read          | `0`                                          |
  | `deletedBy`          | string | UID of the user who deleted the message            | `null`                                       |
  | `deliveredAt`        | number | Epoch timestamp when the message was delivered     | `1745554750`                                 |
  | `deletedAt`          | number | Epoch timestamp when the message was deleted       | `0`                                          |
  | `replyCount`         | number | Number of replies to this message                  | `0`                                          |
  | `sender`             | object | Sender user object                                 | [See below ↓](#edit-message-sender-object)   |
  | `receiverType`       | string | Type of the receiver                               | `"user"`                                     |
  | `editedAt`           | number | Epoch timestamp when the message was edited        | `1745554800`                                 |
  | `parentMessageId`    | number | ID of the parent message (for threads)             | `0`                                          |
  | `readByMeAt`         | number | Epoch timestamp when read by the current user      | `0`                                          |
  | `category`           | string | Message category                                   | `"message"`                                  |
  | `deliveredToMeAt`    | number | Epoch timestamp when delivered to the current user | `0`                                          |
  | `updatedAt`          | number | Epoch timestamp when the message was last updated  | `1745554800`                                 |
  | `unreadRepliesCount` | number | Count of unread replies                            | `0`                                          |
  | `quotedMessageId`    | number | ID of the quoted message                           | `null`                                       |

  ***

  <span id="edit-message-sender-object" style={{scrollMarginTop: '100px'}} />

  **`sender` Object:**

  | Parameter       | Type    | Description                                    | Sample Value                                                            |
  | --------------- | ------- | ---------------------------------------------- | ----------------------------------------------------------------------- |
  | `uid`           | string  | Unique identifier of the sender                | `"cometchat-uid-1"`                                                     |
  | `name`          | string  | Display name of the sender                     | `"Andrew Joseph"`                                                       |
  | `link`          | string  | Profile link                                   | `null`                                                                  |
  | `avatar`        | string  | Avatar URL                                     | `"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-1.webp"` |
  | `metadata`      | object  | Custom metadata                                | `{}`                                                                    |
  | `status`        | string  | Online status                                  | `"online"`                                                              |
  | `role`          | string  | User role                                      | `"default"`                                                             |
  | `statusMessage` | string  | Status message                                 | `null`                                                                  |
  | `tags`          | array   | User tags                                      | `[]`                                                                    |
  | `hasBlockedMe`  | boolean | Whether this user has blocked the current user | `false`                                                                 |
  | `blockedByMe`   | boolean | Whether the current user has blocked this user | `false`                                                                 |
  | `lastActiveAt`  | number  | Epoch timestamp of last activity               | `1745554700`                                                            |

  ***

  <span id="edit-message-receiver-object" style={{scrollMarginTop: '100px'}} />

  **`receiver` Object:**

  | Parameter       | Type    | Description                                    | Sample Value                                                            |
  | --------------- | ------- | ---------------------------------------------- | ----------------------------------------------------------------------- |
  | `uid`           | string  | Unique identifier of the receiver              | `"cometchat-uid-2"`                                                     |
  | `name`          | string  | Display name of the receiver                   | `"George Alan"`                                                         |
  | `link`          | string  | Profile link                                   | `null`                                                                  |
  | `avatar`        | string  | Avatar URL                                     | `"https://assets.cometchat.io/sampleapp/v2/users/cometchat-uid-2.webp"` |
  | `metadata`      | object  | Custom metadata                                | `{}`                                                                    |
  | `status`        | string  | Online status                                  | `"offline"`                                                             |
  | `role`          | string  | User role                                      | `"default"`                                                             |
  | `statusMessage` | string  | Status message                                 | `null`                                                                  |
  | `tags`          | array   | User tags                                      | `[]`                                                                    |
  | `hasBlockedMe`  | boolean | Whether this user has blocked the current user | `false`                                                                 |
  | `blockedByMe`   | boolean | Whether the current user has blocked this user | `false`                                                                 |
  | `lastActiveAt`  | number  | Epoch timestamp of last activity               | `1745550000`                                                            |
</Accordion>

<Accordion title="Error">
  | Parameter | Type   | Description                  | Sample Value                                                   |
  | --------- | ------ | ---------------------------- | -------------------------------------------------------------- |
  | `code`    | string | Error code identifier        | `"ERR_CHAT_API_FAILURE"`                                       |
  | `message` | string | Human-readable error message | `"The message could not be modified."`                         |
  | `details` | string | Additional technical details | `"An unexpected error occurred while processing the request."` |
</Accordion>

The edited message object is returned with `editedAt` (timestamp) and `editedBy` (UID of editor) fields set.

The `editMessage()` method returns a [`BaseMessage`](/sdk/reference/messages#basemessage) object (or a subclass like [`TextMessage`](/sdk/reference/messages#textmessage)).

Relevant fields to access on the returned message:

| Field    | Property   | Return Type | Description                            |
| -------- | ---------- | ----------- | -------------------------------------- |
| editedAt | `editedAt` | `int`       | Timestamp when the message was edited  |
| editedBy | `editedBy` | `String`    | 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

Use `onMessageEdited` in `MessageListener` to receive real-time edit events.

<Tabs>
  <Tab title="Dart">
    ```dart theme={null}
    class Class_Name  with MessageListener {

    //CometChat.addMessageListener("listenerId", this);
    @override
    void onMessageEdited(BaseMessage message) {
      // TODO: implement onMessageEdited
    }


    } 
    ```
  </Tab>
</Tabs>

<Warning>
  Always remove message listeners when they're no longer needed (e.g., in the `dispose()` method). Failing to remove listeners can cause memory leaks and duplicate event handling.

  ```dart theme={null}
  @override
  void dispose() {
    CometChat.removeMessageListener("listenerId");
    super.dispose();
  }
  ```
</Warning>

The `onMessageEdited` callback receives a [`BaseMessage`](/sdk/reference/messages#basemessage) object with the `editedAt` and `editedBy` fields set.

Relevant fields to access on the returned message:

| Field    | Property   | Return Type | Description                            |
| -------- | ---------- | ----------- | -------------------------------------- |
| editedAt | `editedAt` | `int`       | Timestamp when the message was edited  |
| editedBy | `editedBy` | `String`    | UID of the user who edited the message |

## Missed Message Edit Events

When fetching message history, edited messages have `editedAt` and `editedBy` fields set. Additionally, an [`Action`](/sdk/reference/messages#action) message is created when a message is edited.

The [`Action`](/sdk/reference/messages#action) object contains:

* `action` — `edited`
* `actionOn` — Updated message object with the edited details
* `actionBy` — User object containing the details of the user who has edited the message
* `actionFor` — User/group object having the details of the receiver to which the message was sent

<Note>
  You must be the message sender or a group admin/moderator to edit a message.
</Note>

***

## Next Steps

<CardGroup cols={2}>
  <Card title="Delete Message" icon="trash" href="/sdk/flutter/delete-message">
    Learn how to delete messages from conversations
  </Card>

  <Card title="Send Message" icon="paper-plane" href="/sdk/flutter/send-message">
    Send text, media, and custom messages
  </Card>

  <Card title="Receive Messages" icon="inbox" href="/sdk/flutter/receive-messages">
    Handle incoming messages in real-time
  </Card>

  <Card title="Threaded Messages" icon="comments" href="/sdk/flutter/threaded-messages">
    Create and manage message threads
  </Card>
</CardGroup>
