fetchNext method

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

Fetch the list of group members for a group, returns List of GroupMember.

Android Reference: GroupMembersRequest.fetchNext()

Implementation

Future<List<GroupMember>> fetchNext({
  required Function(List<GroupMember> groupMemberList)? onSuccess,
  required Function(CometChatException excep)? onError,
}) async {
  try {
    if (guid.isEmpty) {
      final error = CometChatException(
        ErrorCode.errorInvalidGuid,
        'GUID cannot be empty',
        'GUID cannot be empty',
      );
      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 != 0) {
      if (onSuccess != null) onSuccess([]);
      return [];
    }

    final sdk = SdkRegistry.getInstance();

    final result = await sdk.groupMembers.getGroupMembers(
      guid,
      limit: limit,
      cursor: _cursor != 0 ? _cursor : null,
      page: _nextPage > 0 ? _nextPage : null,
      searchKeyword: searchKeyword,
      scopes: scopes,
      status: status,
    );

    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.nextCursor != null) {
      _cursor = result.nextCursor!;
      key = _cursor.toString();
    }

    final members = result.members;

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