pageable 0.0.2
pageable: ^0.0.2 copied to clipboard
A generic, flexible, API-agnostic pagination container for Flutter and Dart. Supports dynamic field mapping, meta overflow, infinite scroll append, and full JSON serialisation.
import 'package:pageable/pageable.dart';
// 1. Defining your data model
class Product extends BaseData {
final int id;
final String name;
final double price;
const Product({required this.id, required this.name, required this.price});
// Required: Implement fromJson (or equivalent) for Page.fromJson
factory Product.fromJson(Map<String, dynamic> json) {
return Product(
id: json['id'],
name: json['name'],
price: (json['price'] as num).toDouble(),
);
}
// Optional: Implement toJson if you need Page.toJson
Map<String, dynamic> toJson() => {'id': id, 'name': name, 'price': price};
@override
List<Object?> get props => [id, name, price];
}
void main() {
// --- EXAMPLE 1: Basic Usage ---
// API returns standard fields: pageIndex, pageSize, totalResults, numberOfPages, data
final json1 = {
'pageIndex': 0,
'pageSize': 20,
'totalResults': 100,
'numberOfPages': 5,
'data': [
{'id': 1, 'name': 'Laptop', 'price': 999.99},
],
};
final page1 = Page<Product>.fromJson(json1, Product.fromJson);
print(
'Example 1: ${page1.data?.length} products, total: ${page1.totalResults}');
// --- EXAMPLE 2: Custom Field Mapping ---
// API returns different keys: current_page, results, total_count, last_page
final json2 = {
'current_page': 1,
'results': [
{'id': 2, 'name': 'Mouse', 'price': 25.00},
],
'total_count': 50,
'last_page': 10,
};
const config = PageConfig(
pageIndexKey: 'current_page',
dataKey: 'results',
totalResultsKey: 'total_count',
numberOfPagesKey: 'last_page',
);
final page2 = Page<Product>.fromJson(json2, Product.fromJson, config: config);
print('Example 2: Index ${page2.pageIndex}, Count ${page2.totalResults}');
// --- EXAMPLE 3: Infinite Scroll / Append ---
// Merging two pages of data
final page3 = page1.append(page2);
print('Example 3: Appended data length: ${page3.data?.length}'); // 1 + 1 = 2
// --- EXAMPLE 4: Accessing Extra Meta Fields ---
// API returns extra fields like 'next_cursor' not covered by Page properties
final json4 = {
'pageIndex': 2,
'data': [],
'next_cursor': 'Ym9va21hcmsxMjM=',
'has_more': true,
};
final page4 = Page<Product>.fromJson(json4, Product.fromJson);
final cursor = page4.meta['next_cursor'];
final hasMore = page4.meta['has_more'];
print('Example 4: Next Cursor: $cursor, Has More: $hasMore');
// --- EXAMPLE 5: copyWith Usage ---
// Updating navigation state locally
final nextPage = page1.copyWith(
meta: {...page1.meta, 'pageIndex': page1.pageIndex + 1},
);
print(
'Example 5: Old Index: ${page1.pageIndex}, New Index: ${nextPage.pageIndex}');
// --- EXAMPLE 6: (Bonus) Typed Extension Getters ---
// Adding custom getters for meta fields without changing the package
// See extension below
print('Example 6: Strongly typed cursor: ${page4.nextCursor}');
}
// Extension to provide typed access to meta fields
extension ProductPageExtension on Page<Product> {
String? get nextCursor => meta['next_cursor'] as String?;
bool get hasMore => meta['has_more'] as bool? ?? false;
}