fetchNext method

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

Returns list of Group object fetched after putting the filters.

Android Reference: GroupsRequest.fetchNext()

Implementation

Future<List<Group>> fetchNext({
  required Function(List<Group> groupList)? 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 (_nextPage > _totalPages && _totalPages != 0) {
      if (onSuccess != null) onSuccess([]);
      return [];
    }

    if (_inProgress) {
      if (onSuccess != null) onSuccess([]);
      return [];
    }

    _inProgress = true;

    final sdk = SdkRegistry.getInstance();

    final result = await sdk.groups.getGroups(
      limit: limit,
      token: _cursor != 0 ? _cursor : null,
      page: _nextPage > 0 ? _nextPage : null,
      searchKeyword: searchKeyword,
      joinedOnly: joinedOnly,
      tags: tags,
      withTags: withTags,
    );

    if (result.pagination != null) {
      if (result.pagination!.totalPages != null) {
        _totalPages = result.pagination!.totalPages!;
      }
      if (result.pagination!.currentPage != null) {
        _currentPage = result.pagination!.currentPage!;
        _nextPage = _currentPage + _pageOffset;
      }
    }

    if (result.nextToken != null) {
      _cursor = result.nextToken!;
      key = _cursor.toString();
    }

    _inProgress = false;

    final groups = result.groups;

    if (onSuccess != null) onSuccess(groups);
    return groups;
  } on 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 [];
}