PaginationResponse<T>.fromPageBased constructor
PaginationResponse<T>.fromPageBased (})
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,
);
}