Created
June 14, 2023 10:43
-
-
Save 7ANV1R/ba49ffb6ae131943f48365876c5cf068 to your computer and use it in GitHub Desktop.
Pagination with Riverpod FutureProvider API data (infinite_scroll_pagination+riverpod)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// import 'package:flutter/material.dart'; | |
// import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart'; | |
// import 'package:riverpod/riverpod.dart'; | |
// Import properly | |
// Mock data model representing a property | |
class Property { | |
final int id; | |
final String name; | |
Property(this.id, this.name); | |
} | |
// Mock API service for fetching properties | |
class PropertyApi { | |
Future<List<Property>> fetchProperties(int pageNumber, int pageSize) async { | |
// Simulating an API call delay | |
await Future.delayed(Duration(seconds: 2)); | |
// Generate mock properties | |
final properties = List.generate( | |
pageSize, | |
(index) => Property( | |
index + pageNumber * pageSize, | |
'Property ${index + pageNumber * pageSize}', | |
), | |
); | |
return properties; | |
} | |
} | |
// Provider for the PropertyApi instance | |
final propertyApiProvider = Provider((ref) => PropertyApi()); | |
// Provider for the FutureProvider | |
final propertiesProvider = FutureProvider.autoDispose<PagingController<int, Property>>((ref) { | |
final propertyApi = ref.read(propertyApiProvider); | |
final controller = PagingController<int, Property>(firstPageKey: 0); | |
controller.addPageRequestListener((pageKey) { | |
propertyApi.fetchProperties(pageKey, 10).then((newProperties) { | |
final isLastPage = newProperties.length < 10; | |
if (isLastPage) { | |
controller.appendLastPage(newProperties); | |
} else { | |
final nextPageKey = pageKey + 1; | |
controller.appendPage(newProperties, nextPageKey); | |
} | |
}).catchError((error) { | |
controller.error = error; | |
}); | |
}); | |
return controller; | |
}); | |
void main() { | |
runApp(ProviderScope(child: MyApp())); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Property List', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Property List'), | |
), | |
body: PropertyList(), | |
), | |
); | |
} | |
} | |
class PropertyList extends ConsumerWidget { | |
@override | |
Widget build(BuildContext context, WidgetRef ref) { | |
final pagingController = ref.watch(propertiesProvider); | |
return pagingController.when( | |
data: (properties) => PagedListView<int, Property>( | |
pagingController: properties, | |
builderDelegate: PagedChildBuilderDelegate<Property>( | |
itemBuilder: (context, property, index) => ListTile( | |
title: Text(property.name), | |
subtitle: Text('ID: ${property.id}'), | |
), | |
), | |
), | |
loading: () => Center(child: CircularProgressIndicator()), | |
error: (error, stackTrace) => Center(child: Text('Error: $error')), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment