fetchNext method

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

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,
  GroupMemberFetchListener? listener,
}) async {
  // Released just before the callbacks, never after them: a fetch made
  // from inside the listener's own callback is a new fetch, not a concurrent
  // one (BEH-READ-09a).
  var counted = false;
  void release() {
    if (counted) {
      counted = false;
      _inFlight--;
    }
  }

  // Whether offline is running for this fetch. With offline off a two-phase
  // read behaves exactly as before the cache existed (BEH-AUTH-05).
  final offlineRunning = OfflineRuntime.instance.store != null;
  try {
    if (guid.isEmpty) {
      final error = CometChatException(
        ErrorCode.errorInvalidGuid,
        'GUID cannot be empty',
        'GUID cannot be empty',
      );
      if (onError != null) onError(error);
      listener?.onError(error);
      return [];
    }

    if (limit > _maxLimit) {
      final error = CometChatException(
        ErrorCode.errorLimitExceeded,
        'Limit cannot exceed $_maxLimit',
        'Limit cannot exceed $_maxLimit',
      );
      if (onError != null) onError(error);
      listener?.onError(error);
      return [];
    }

    // Past the end of history a two-phase read with offline running still
    // asks the server: an empty answer ends the list, no answer is the
    // not-connected error (BEH-READ-05, BEH-READ-09a, R105). A legacy caller,
    // or any caller with offline off, keeps the empty page it always got.
    if ((listener == null || !offlineRunning) &&
        _nextPage > _totalPages &&
        _totalPages != 0) {
      if (onSuccess != null) onSuccess([]);
      listener?.onServerGroupMembers(const []);
      return [];
    }

    // A concurrent two-phase fetch with offline running answers an empty
    // success (BEH-SNAP-14). With offline off it goes out, as it always did.
    if (listener != null && offlineRunning && _inFlight > 0) {
      if (onSuccess != null) onSuccess([]);
      listener.onServerGroupMembers(const []);
      return [];
    }
    _inFlight++;
    counted = true;

    // Phase 1 — replay a recorded page for this query. The snapshot's
    // replay index is independent of the request's page cursor, so a
    // replay never advances the server cursor.
    if (listener != null) {
      final cached = await _snapshots.cachedPhase(GroupMember.fromMap);
      if (cached != null) listener.onCachedGroupMembers(cached);
    }

    final sdk = SdkRegistry.getInstance();

    // Captured as the request goes out: a page answered after this
    // session ended is never written (BEH-SYNC-06).
    final issuedIn = OfflineRuntime.instance.readSession;
    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;

    // Record before delivering, so a caller that re-reads immediately sees
    // the same page.
    await _snapshots.recordEntities(
      members,
      hasNext: _totalPages != 0 && _nextPage <= _totalPages,
      issuedIn: issuedIn,
      toJson: (m) => (m as GroupMember).toJson(),
    );

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