fetchNext method

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

Returns a list of Conversation object fetched after putting the filters.

Android Reference: ConversationsRequest.fetchNext()

Implementation

Future<List<Conversation>> fetchNext(
    {required Function(List<Conversation> message)? onSuccess,
    required Function(CometChatException excep)? onError}) async {
  try {
    final effectiveLimit = limit ?? defaultLimit;
    if (effectiveLimit <= 0) {
      final error = CometChatException(
        'ERR_NON_POSITIVE_LIMIT',
        'Limit must be a positive number',
        'Limit must be a positive number',
      );
      if (onError != null) onError(error);
      return [];
    }

    if (effectiveLimit > maxLimit) {
      final error = CometChatException(
        'ERR_LIMIT_EXCEEDED',
        'Limit cannot exceed $maxLimit',
        'Limit cannot exceed $maxLimit',
      );
      if (onError != null) onError(error);
      return [];
    }

    if ((_nextPage == -1 || _nextPage <= _totalPages) && !_inProgress) {
      _inProgress = true;

      final sdk = SdkRegistry.getInstance();

      final result = await sdk.conversations.getConversations(
        limit: effectiveLimit,
        page: _nextPage != -1 ? _nextPage : null,
        conversationType: conversationType,
        withUserAndGroupTags: withUserAndGroupTags,
        withTags: withTags,
        tags: tags,
        userTags: userTags,
        groupTags: groupTags,
        includeBlockedUsers: includeBlockedUsers,
        withBlockedInfo: withBlockedInfo,
        searchKeyword: searchKeyword,
        unread: unread,
        hideAgentic: hideAgentic,
        onlyAgentic: onlyAgentic,
      );

      _totalPages = result.totalPages;
      _currentPage = result.currentPage;
      _nextPage = _currentPage + _pageOffset;

      key = _currentPage.toString();
      _inProgress = false;

      if (onSuccess != null) {
        onSuccess(result.conversations);
      }
      return result.conversations;
    } else {
      if (onSuccess != null) {
        onSuccess([]);
      }
      return [];
    }
  } 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 [];
}