PaginationResponse<T>.fromCursorBased constructor

PaginationResponse<T>.fromCursorBased(
  1. Map<String, dynamic> json,
  2. T itemFromJson(
    1. Map<String, dynamic>
    ), {
  3. String dataKey = 'data',
  4. String paginationKey = 'pagination',
  5. String nextCursorKey = 'next_cursor',
  6. String hasMoreKey = 'has_more',
})

Creates a PaginationResponse for cursor-based pagination

This factory supports APIs that use cursor-based pagination:

{
  "data": [...],
  "pagination": {
    "next_cursor": "cursor_string",
    "has_more": true
  }
}

Implementation

factory PaginationResponse.fromCursorBased(
  Map<String, dynamic> json,
  T Function(Map<String, dynamic>) itemFromJson, {
  String dataKey = 'data',
  String paginationKey = 'pagination',
  String nextCursorKey = 'next_cursor',
  String hasMoreKey = 'has_more',
}) {
  final items = json[dataKey] as List<dynamic>;
  final itemList = items
      .map((item) => itemFromJson(item as Map<String, dynamic>))
      .toList();
  final pagination = json[paginationKey] as Map<String, dynamic>?;
  final hasMore = pagination?[hasMoreKey] as bool? ?? false;
  final nextCursor = pagination?[nextCursorKey];

  return PaginationResponse<T>(
    items: itemList,
    total: -1, // Unknown for cursor-based pagination
    skip: 0, // Not applicable for cursor-based
    limit: itemList.length,
    isLastPage: !hasMore,
    nextPageKey: hasMore ? nextCursor : null,
  );
}