spotube/lib/hooks/usePaginatedFutureProvider.dart
Kingkor Roy Tirtho 74df7a91b1 PlayerOverlay flickering fix
track Share popup menu not showing when not logged in fix
page tranistions changed to default material 3 design's
Wrong link in Login page fix
Changing UserPreferences provider resets the Playback provider
using using hookified_infinite_scroll_pagination officially
2022-06-07 19:53:33 +06:00

43 lines
1.3 KiB
Dart

import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hookified_infinite_scroll_pagination/hookified_infinite_scroll_pagination.dart';
PagingController<P, ItemType> usePaginatedFutureProvider<T, P, ItemType>(
FutureProvider<T> Function(P pageKey) createSnapshot, {
required P firstPageKey,
required WidgetRef ref,
void Function(
T,
PagingController<P, ItemType> pagingController,
P pageKey,
)?
onData,
void Function(Object)? onError,
void Function()? onLoading,
}) {
final currentPageKey = useState(firstPageKey);
final snapshot = ref.watch(createSnapshot(currentPageKey.value));
final pagingController = usePagingController<P, ItemType>(
firstPageKey: firstPageKey,
onPageRequest: (pageKey, pagingController) {
if (currentPageKey.value != pageKey) {
currentPageKey.value = pageKey;
}
});
useEffect(() {
snapshot.whenOrNull(
data: (data) =>
onData?.call(data, pagingController, currentPageKey.value),
error: (error, _) {
pagingController.error = error;
return onError?.call(error);
},
loading: onLoading,
);
return null;
}, [currentPageKey, snapshot]);
return pagingController;
}