sendMessage static method

Future<TextMessage?> sendMessage(
  1. TextMessage message, {
  2. required dynamic onSuccess(
    1. TextMessage message
    )?,
  3. required dynamic onError(
    1. CometChatException excep
    )?,
})

To send a text message to a single user or group. returns the TextMessage with message id

receiver The UID or GUID of the recipient

messageText The text to be sent

receiverType The type of the receiver to whom the message is to be sent i.e user or group

The method could throw PlatformException with error codes specifying the cause

Implementation

static Future<TextMessage?> sendMessage(TextMessage message, {required Function(TextMessage message)? onSuccess, required Function(CometChatException excep)? onError}) async {
  try {
    final result = await channel.invokeMethod('sendMessage', {
      'receiverId': message.receiverUid,
      'receiverType': message.receiverType,
      'messageText': message.text,
      'parentMessageId': message.parentMessageId,
      'metadata': json.encode(message.metadata ?? {}),
      'tags': message.tags,
      'muid': message.muid,
    });
    final TextMessage res = TextMessage.fromMap(result);
    if(onSuccess != null) onSuccess(res);
    return res;
  } 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()));
  }
  return null;
}