PaginationResponse<T>.fromPageBased constructor

PaginationResponse<T>.fromPageBased(
  1. Map<String, dynamic> json,
  2. T itemFromJson(
    1. Map<String, dynamic>
    ), {
  3. String dataKey = 'data',
  4. String currentPageKey = 'current_page',
  5. String lastPageKey = 'last_page',
  6. String perPageKey = 'per_page',
  7. String totalKey = 'total',
})

Creates a PaginationResponse for page-based pagination

This factory supports APIs that use page numbers:

{
  "data": [...],
  "current_page": 1,
  "last_page": 10,
  "per_page": 15,
  "total": 150
}

Implementation

factory PaginationResponse.fromPageBased(
  Map<String, dynamic> json,
  T Function(Map<String, dynamic>) itemFromJson, {
  String dataKey = 'data',
  String currentPageKey = 'current_page',
  String lastPageKey = 'last_page',
  String perPageKey = 'per_page',
  String totalKey = 'total',
}) {
  final items = json[dataKey] as List<dynamic>;
  final itemList = items
      .map((item) => itemFromJson(item as Map<String, dynamic>))
      .toList();
  final currentPage = json[currentPageKey] as int;
  final lastPage = json[lastPageKey] as int;
  final perPage = json[perPageKey] as int;
  final total = json[totalKey] as int;

  return PaginationResponse<T>(
    items: itemList,
    total: total,
    skip: (currentPage - 1) * perPage,
    limit: perPage,
    isLastPage: currentPage >= lastPage,
    nextPageKey: currentPage < lastPage ? currentPage + 1 : null,
  );
}