sendFormMessage static method

Future<FormMessage?> sendFormMessage(
  1. FormMessage message, {
  2. dynamic onSuccess(
    1. FormMessage
    )?,
  3. dynamic onError(
    1. CometChatException
    )?,
})

sendFormMessage used to send a custom message

Implementation

static Future<FormMessage?> sendFormMessage(
  FormMessage message, {
  dynamic Function(FormMessage)? onSuccess,
  dynamic Function(CometChatException)? onError,
}) async {
  if (message.parentMessageId == -1) {
    message.parentMessageId = 0;
  }

  message.sender ??= loggedInUser;
  if (message.muid.trim().isEmpty) {
    message.muid = DateTime.now().microsecondsSinceEpoch.toString();
  }

  CometChatMessageEvents.ccMessageSent(message, MessageStatus.inProgress);
  FormMessage? result = await SDKMethods.sendFormMessage(message,
      onSuccess: (FormMessage sentMessage) {
    //executing the custom onSuccess handler
    if (onSuccess != null) {
      try {
        onSuccess(sentMessage);
      } catch (e) {
        if (kDebugMode) {
          debugPrint(
              "message sent successfully but failed to execute onSuccess callback");
        }
      }
    }
    //the ccMessageSent event is emitted to update the message receipt shown
    //in the footer of the message bubble from in progress to sent
    CometChatMessageEvents.ccMessageSent(sentMessage, MessageStatus.sent);
  }, onError: (error) {
    //executing the custom onError handler
    if (onError != null) {
      try {
        onError(error);
      } catch (e) {
        if (kDebugMode) {
          debugPrint(
              "message could not be sent and failed to execute onError callback");
        }
      }
    }
    //a error property is added to the metadata of the message
    //because of which a error message receipt will be shown in the
    //footer of the message bubble in the message list
    if (message.metadata != null) {
      message.metadata!["error"] = error;
    } else {
      message.metadata = {"error": error};
    }
    CometChatMessageEvents.ccMessageSent(message, MessageStatus.error);
  });
  return result;
}