fetchNext method

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

Fetches the next page of notification feed items.

Android Reference: NotificationFeedRequest.fetchNext()

Manages cursor internally. When server returns no cursor, subsequent calls return an empty list without making a network request.

Returns: List of NotificationFeedItem objects

Throws:

Implementation

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

    // If cursor exhausted, return empty list
    if (!_hasMore && !_isFirstFetch) {
      if (onSuccess != null) onSuccess([]);
      return [];
    }

    // Prevent concurrent fetches
    if (_inProgress) {
      if (onSuccess != null) onSuccess([]);
      return [];
    }

    _inProgress = true;

    final sdk = SdkRegistry.getInstance();

    final result = await sdk.notificationFeed.fetchFeed(
      limit: effectiveLimit,
      cursor: _isFirstFetch ? null : _cursor,
      readState: readState?.value,
      category: category,
      channelId: channelId,
      tags: tags,
      dateFrom: dateFrom,
      dateTo: dateTo,
    );

    // Update pagination state
    _isFirstFetch = false;
    _cursor = result.cursor;
    _hasMore = result.cursor != null;
    _inProgress = false;

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