fetchNext method

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

Fetch a list of users based on the provided filters.

Android Reference: UsersRequest.fetchNext()

Implementation

Future<List<User>> fetchNext({
  required Function(List<User> userList)? onSuccess,
  required Function(CometChatException excep)? onError,
}) async {
  try {
    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 (limit > _maxLimit) {
      final error = CometChatException(
        ErrorCode.errorLimitExceeded,
        'Limit cannot exceed $_maxLimit',
        'Limit cannot exceed $_maxLimit',
      );
      if (onError != null) onError(error);
      return [];
    }

    if (_nextPage > _totalPages && _totalPages != -1) {
      if (onSuccess != null) onSuccess([]);
      return [];
    }

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

    _inProgress = true;

    final sdk = SdkRegistry.getInstance();

    final result = await sdk.users.getUsers(
      limit: limit,
      token: _token != 0 ? _token : null,
      page: _nextPage > 0 ? _nextPage : null,
      searchKeyword: searchKeyword,
      status: userStatus,
      hideBlockedUsers: hideBlockedUsers,
      roles: roles,
      friendsOnly: friendsOnly,
      tags: tags,
      withTags: withTags,
      uids: uids,
      searchInFields: searchIn,
      sortBy: sortBy,
      sortOrder: sortByOrder,
    );

    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) {
      _token = result.nextToken!;
      key = _token.toString();
    }

    _inProgress = false;

    final users = result.users;

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