Untitled
unknown
plain_text
a month ago
1.3 kB
2
Indexable
class PaginationService { int _currentPage = 1; int _limit = 1; bool _hasMoreData = true; PaginationService({ int limit = 20, }) : _limit = limit; Future<List<String>> getNextPage() async { // Return empty list if no more data if (!_hasMoreData) { return []; } try { // Fetch data using the provided fetchData function final List<String> data = await _fetchData(_currentPage, _limit); // Check if there is no more data available if (data.isEmpty || data.length < _limit) { _hasMoreData = false; } // Increment current page for next request _currentPage++; return data; } catch (e) { // Rethrow any errors that occur rethrow; } } void resetPagination() { _currentPage = 1; _hasMoreData = true; } bool hasMoreData() => _hasMoreData; void setLimit(int newLimit) { _limit = newLimit; } Future<List<String>> _fetchData(int page, int limit) async { await Future.delayed(const Duration(seconds: 1)); // Simulate network delay // Simulate no more data after page 3 if (page > 3) return []; return List.generate( limit, (index) => 'Item ${(page - 1) * limit + index + 1}', ); } }
Editor is loading...
Leave a Comment