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

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 {
    late Map<String, dynamic> messageMap;
    if (message is TextMessage) {
      messageMap = (message as TextMessage).toJson();
    } else if (message is CustomMessage) {
      messageMap = (message as CustomMessage).toJson();
    } else {
      messageMap = message.toJson();
    }
    final result = await channel.invokeMethod('editMessage', {'baseMessage': messageMap});
    late BaseMessage resultMessage;
    switch (message.type) {
      case CometChatMessageType.text:
        resultMessage = TextMessage.fromMap(result);
        break;
      case CometChatMessageType.custom:
        resultMessage = CustomMessage.fromMap(result);
        break;
      default:
        resultMessage = BaseMessage.fromMap(result);
        break;
    }
    if(onSuccess != null) onSuccess(resultMessage);
  } on PlatformException catch (platformException) {
    if(onError != null) onError(CometChatException(platformException.code, platformException.details, platformException.message));
  } catch (e) {
    debugPrint("Error: $e");
    if(onError != null) onError(CometChatException(ErrorCode.errorUnhandledException, e.toString() , e.toString()));
  }
}