fetchNext method

Future<List<Reaction>> fetchNext({
  1. required dynamic onSuccess(
    1. List<Reaction> message
    )?,
  2. required dynamic onError(
    1. CometChatException excep
    )?,
})

Asynchronously fetches the next set of reactions for a particular message.

Android Reference: ReactionsRequest.fetchNext()

Implementation

Future<List<Reaction>> fetchNext({
  required Function(List<Reaction> message)? onSuccess,
  required Function(CometChatException excep)? onError,
}) async {
  try {
    if (limit > _maxLimit) {
      final error = CometChatException(
        ErrorCode.errorLimitExceeded,
        'Limit cannot exceed $_maxLimit',
        'Limit cannot exceed $_maxLimit',
      );
      if (onError != null) onError(error);
      return [];
    }

    if (limit <= 0) {
      final error = CometChatException(
        ErrorCode.errorNonPositiveLimit,
        'Limit must be a positive number',
        'Limit must be a positive number',
      );
      if (onError != null) onError(error);
      return [];
    }

    if (messageId <= 0) {
      final error = CometChatException(
        ErrorCode.errorInvalidMessageId,
        'Invalid message ID',
        'Invalid message ID',
      );
      if (onError != null) onError(error);
      return [];
    }

    if (_inProgress) {
      final error = CometChatException(
        ErrorCode.errorRequestInProgress,
        'Request already in progress',
        'Request already in progress',
      );
      if (onError != null) onError(error);
      return [];
    }

    _inProgress = true;

    final sdk = SdkRegistry.getInstance();

    final result = await sdk.reactions.getReactions(
      messageId.toString(),
      limit: limit,
      affix: 'append',
      paginationReactionId: _nextReactionId,
      reaction: reaction,
    );

    _inProgress = false;

    // **Android Reference:** hasPrevious set based on KEY_PAGINATION_PREVIOUS key existence
    _hasPrevious = result.hasPreviousPage;
    if (result.previousReactionId != null) {
      _previousReactionId = result.previousReactionId;
    }

    if (result.nextReactionId != null) {
      _nextReactionId = result.nextReactionId;
    }

    final reactions = result.reactions.map((reactionData) {
      return Reaction(
        id: reactionData.id,
        messageId: reactionData.messageId ?? messageId,
        reaction: reactionData.reaction,
        uid: reactionData.uid,
        reactedBy: User.fromMap(reactionData.reactedBy),
        reactedAt: reactionData.reactedAt,
      );
    }).toList();

    if (onSuccess != null) {
      onSuccess(reactions);
    }
    return reactions;
  } on sdk_errors.SdkException catch (sdkEx) {
    _inProgress = false;
    final cometChatEx = CometChatException(
      sdkEx.code,
      sdkEx.details ?? sdkEx.message,
      sdkEx.message,
    );
    if (onError != null) {
      onError(cometChatEx);
    }
  } catch (e) {
    _inProgress = false;
    final cometChatEx = CometChatException(
      ErrorCode.errorUnhandledException,
      e.toString(),
      e.toString(),
    );
    if (onError != null) {
      onError(cometChatEx);
    }
  }
  return [];
}