I want to release everything when leaving the current screen.
Getx advising me to use onClose
method of GetxController
for this.
@override
void onClose() {
pagingController.dispose();
super.onClose();
}
He kept his promise, so nice. But it didn't take long! Let's dive in;
The controller has a future method called _fetchPage
, it basically fetches page data and works with pagingController
If I leave the screen before completing the _fetchPage
, the future remains working. Once the data is fetched, pagingController
is accessed but it has been already disposed.
And finally, it prints Unhandled Exception: Exception: A PagingController was used after being disposed.
Future<void> _fetchPage(int pageKey) async {
try {
var skip = pageKey == 0 ? 0 : (10 * pageKey);
var data = await fetchDataOfPage(skip, limit);
final isLastPage = data.length < limit;
if (isLastPage) {
pagingController.appendLastPage(data);
} else {
final nextPageKey = pageKey + 1;
pagingController.appendPage(data, nextPageKey);
}
} catch (error) {
pagingController.error = error;
}
}