addReaction static method

Future<BaseMessage?> addReaction(
  1. int messageId,
  2. String reaction, {
  3. required dynamic onSuccess(
    1. BaseMessage
    )?,
  4. required dynamic onError(
    1. CometChatException excep
    )?,
})

Add a reaction to a message.

Migration Note: Migrated from platform channels to native Dart. Uses ReactionRepository.addReaction internally.

Android Reference: CometChat.addReaction(int messageId, String reaction)

The messageId parameter specifies the ID of the message to add the reaction to. The reaction parameter specifies the reaction emoji to add. The onSuccess function is called with the updated BaseMessage when the reaction is added successfully. The onError function is called with a CometChatException object when there is an error while adding the reaction.

Returns a Future that resolves to the updated BaseMessage or null if there was an error.

Implementation

static Future<BaseMessage?> addReaction(int messageId, String reaction,
    {required Function(BaseMessage)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    // 1. Get SDK instance
    final sdk = SdkRegistry.getInstance();

    // 2. Call repository (convert int messageId to String)
    final updatedMessage = await sdk.reactions.addReaction(
      messageId.toString(),
      reaction,
    );

    // 3. Call success callback
    if (onSuccess != null) onSuccess(updatedMessage);
    return updatedMessage;
  } on SdkException catch (sdkEx) {
    // 4. Convert SdkException to CometChatException
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    _errorCallbackHandler(cometChatEx, null, null, onError);
  } catch (e) {
    _errorCallbackHandler(null, null, e, onError);
  }
  return null;
}