cursorPaginate method

  1. @override
Future<Map<String, dynamic>> cursorPaginate({
  1. int perPage = 15,
  2. String? cursor,
  3. String column = 'id',
})
override

Cursor-based pagination for efficient large dataset navigation

Implementation

@override
Future<Map<String, dynamic>> cursorPaginate({
  int perPage = 15,
  String? cursor,
  String column = 'id',
}) async {
  final q = clone();
  q.limit(perPage + 1);

  if (cursor != null) {
    q.where(column, '>', cursor);
  }

  q.orderBy(column, direction: 'asc');

  final results = await q.get();

  String? nextCursor;
  List<T> items = results;

  if (results.length > perPage) {
    items = results.sublist(0, perPage);
    final lastItem = items.last;
    if (lastItem is Map) {
      nextCursor = lastItem[column]?.toString();
    } else if (lastItem is KhademModel) {
      nextCursor = (lastItem as KhademModel).getAttribute(column)?.toString();
    } else {
      try {
        nextCursor = (lastItem as dynamic)[column]?.toString();
      } catch (_) {
        throw Exception(
          'Could not determine the value of the cursor column "$column" from the result.',
        );
      }
    }
  }

  return {
    'data': items,
    'per_page': perPage,
    'next_cursor': nextCursor,
    'prev_cursor': null,
  };
}