fetchNext method
Future<List<NotificationFeedItem> >
fetchNext({
- required dynamic onSuccess(
- List<
NotificationFeedItem> feedItems
- List<
- required dynamic onError(
- CometChatException excep
- NotificationFeedFetchListener? listener,
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:
- CometChatException on network or HTTP errors
Implementation
Future<List<NotificationFeedItem>> fetchNext({
required Function(List<NotificationFeedItem> feedItems)? onSuccess,
required Function(CometChatException excep)? onError,
NotificationFeedFetchListener? listener,
}) 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);
listener?.onError(error);
return [];
}
if (effectiveLimit > maxLimit) {
final error = CometChatException(
ErrorCode.errorLimitExceeded,
'Limit cannot exceed $maxLimit',
'Limit cannot exceed $maxLimit',
);
if (onError != null) onError(error);
listener?.onError(error);
return [];
}
// A read past the end, two-phase with offline running, still makes its
// request (BEH-READ-05, R105/R120): it re-sends the last page's request
// and delivers only items not delivered yet — an empty answer is the end,
// no answer the not-connected error. A legacy caller, or any caller with
// offline off, keeps the empty page it always got (BEH-AUTH-05).
final pastTheEnd = !_hasMore && !_isFirstFetch;
final readsPastTheEnd =
pastTheEnd &&
listener != null &&
OfflineRuntime.instance.store != null;
// Both guards settle the listener the same way they settle `onSuccess` —
// it is a drop-in for it, so a listener-only caller must not be left
// waiting on a call that already returned.
if (pastTheEnd && !readsPastTheEnd) {
if (onSuccess != null) onSuccess([]);
listener?.onServerNotificationFeedItems(const []);
return [];
}
// A concurrent two-phase fetch with offline running is refused with
// ERROR_REQUEST_IN_PROGRESS (BEH-SNAP-14, R27). A legacy caller, or any
// caller with offline off, keeps the empty page it always got here
// (BEH-AUTH-05).
if (_inProgress) {
if (listener != null && OfflineRuntime.instance.store != null) {
final error = CometChatException(
ErrorCode.errorRequestInProgress,
'Request already in progress',
'Request already in progress',
);
if (onError != null) onError(error);
listener.onError(error);
return [];
}
if (onSuccess != null) onSuccess([]);
listener?.onServerNotificationFeedItems(const []);
return [];
}
// Set before the cache phase: a fetch called while this one is in
// flight fires no cache phase and leaves the replay index alone
// (BEH-SNAP-14).
_inProgress = true;
// Phase 1 — replay a recorded page. The replay index is independent of
// the request's cursor, so a replay never advances it.
if (listener != null) {
final cached = await _snapshots.cachedPhase(
NotificationFeedItem.fromMap,
);
if (cached != null) listener.onCachedNotificationFeedItems(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 cursor = readsPastTheEnd
? _lastPageCursor
: (_isFirstFetch ? null : _cursor);
final result = await sdk.notificationFeed.fetchFeed(
limit: effectiveLimit,
cursor: cursor,
readState: readState?.value,
category: category,
channelId: channelId,
tags: tags,
dateFrom: dateFrom,
dateTo: dateTo,
);
if (readsPastTheEnd) {
// The last page again: only what this request has not delivered goes
// out, and nothing is recorded (R120). A cursor in the answer means a
// page now follows it, which the next fetch continues to.
final fresh = result.items
.where((i) => !_deliveredIds.contains(i.id))
.toList(growable: false);
_deliveredIds.addAll(fresh.map((i) => i.id));
if (result.cursor != null) {
_cursor = result.cursor;
_hasMore = true;
}
_inProgress = false;
if (onSuccess != null) onSuccess(fresh);
listener.onServerNotificationFeedItems(fresh);
return fresh;
}
// Update pagination state
_isFirstFetch = false;
_lastPageCursor = cursor;
_cursor = result.cursor;
_hasMore = result.cursor != null;
_deliveredIds.addAll(result.items.map((i) => i.id));
// Record before delivering, so a caller that re-reads immediately sees
// the same page.
await _snapshots.recordEntities(
result.items,
hasNext: _hasMore,
issuedIn: issuedIn,
toJson: (i) => (i as NotificationFeedItem).toJson(),
);
// Held through the record step, so a fetch landing during it cannot
// replay the page about to be delivered (BEH-SNAP-14); cleared before
// the callbacks, so a fetch made from one of them is a new fetch.
_inProgress = false;
if (onSuccess != null) {
onSuccess(result.items);
}
listener?.onServerNotificationFeedItems(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);
}
// A listener-only caller is told too, or it waits forever offline
// (BEH-READ-05).
listener?.onError(cometChatEx);
} catch (e) {
_inProgress = false;
final cometChatEx = CometChatException(
ErrorCode.errorUnhandledException,
e.toString(),
e.toString(),
);
if (onError != null) {
onError(cometChatEx);
}
// A listener-only caller is told too, or it waits forever offline
// (BEH-READ-05).
listener?.onError(cometChatEx);
}
return [];
}