Pagination with AsyncNotifierProvider (and unit tests) #2763
Replies: 1 comment
-
Hi there! It appears to me that you don't care about whether the pagination has been ended or not "if the provider has fetched the last page from the data source and there is no more loading" (because your state = const AsyncLoading();
/// or [state = const AsyncValue.loading();] which as a factory of [AsyncLoading] That will copy the state with loading, but, if you are using the provider as: // or even the matching pattern to achieve the same result
ref.watch(provider).when() Then every time you fetch new todos using /// Even if [isLoading] is true, it is still possible for [hasValue]/[hasError]
/// to also be true.
bool get isLoading;
/// Whether [value] is set.
///
/// Even if [hasValue] is true, it is still possible for [isLoading]/[hasError]
/// to also be true.
bool get hasValue; Here is the extension for you: import 'package:flutter_riverpod/flutter_riverpod.dart';
extension AsyncValueExtension<T> on AsyncValue<T> {
R paginationWhen<R>({
// set this to true if you want to skip loading when hasValue is true
bool skipLoadingOnHasValue = false,
bool skipLoadingOnReload = false,
bool skipLoadingOnRefresh = true,
bool skipError = false,
required R Function(T data) data,
required R Function(Object error, StackTrace stackTrace) error,
required R Function() loading,
}) {
if (skipLoadingOnHasValue) {
if (hasValue && isLoading) {
return data(requireValue);
}
}
return when(
skipLoadingOnReload: skipLoadingOnReload,
skipLoadingOnRefresh: skipLoadingOnRefresh,
skipError: skipError,
data: (d) => data(d),
error: error,
loading: loading,
);
}
}
And in your widget you can use it as the 'when()' but with one extra optional parameter "By default is false so make sure you set it as true" as: ref.watch(provider).paginationWhen(
skipLoadingOnHasValue: true,
data: (data) => DataContentWidget(),
error: (error,stackTrace) => ErrorWidget(),
loading: () => LoadingWidget(),
) That should solve the problem you're facing. Also, you can customize it to handle cases such as Also, see this repo https://github.com/K9i-0/riverpod_paging_sample |
Beta Was this translation helpful? Give feedback.
-
Hey all,
I'm trying to build an AsyncNotifierProvider that supports pagination.
I'm not sure if I'm setting up the providers properly and I'm having some problems testing them.
I've created a small demo below and interleaved the main questions (copied here for convenience):
I'm essentially trying to replicate this but with the new and recommended AsyncNotifierProvider. I'm also trying to unit test this, which that article does not cover.
I've got this like 90% of the way there, but (1) mainly just want confirmation that I'm using the loading state properly when fetching more items and (2) need help with the failing unit test
Here is the small demo source code. This can be copied into a test file:
demo_test.dart
and after generatingdemo_test.g.dart
, should be good toflutter test
.Thanks in advance!!
Beta Was this translation helpful? Give feedback.
All reactions