Last updated:
0 purchases
fuery core
Fuery Core #
Asynchronous State Management for Dart. #
Features #
Async data fetching, caching, invalidation, pagination
Mutation with side effect
Installation #
flutter pub add fuery_core
copied to clipboard
Basic Usage #
Query #
int id = 2;
// QueryResult<Post, Error>
late final post = Query.use<Post, Error>(
queryKey: ['posts', id],
queryFn: () => repository.getPostById(id),
);
copied to clipboard
Infinite Query #
class PageResponse<T> {
final List<T> items;
final int? nextCursor;
...
factory PageResponse.fromJson(Map<String, dynamic> map) {
return ...;
}
}
class MyRepository {
Future<PageResponse<Post>> getPostsByPage(int page) async {
try {
return PageResponse.fromJson(...);
} catch(_) {
throw Error();
}
}
}
// InfiniteQueryResult<int, List<InfiniteData<int, PageResponse<Post>>>, Error>
late final posts = InfiniteQuery.use<int, PageResponse<Post>, Error>(
queryKey: ['posts', 'list'],
queryFn: (int page) => repository.getPostsByPage(page),
initialPageParam: 1,
getNextPageParam: (lastPage, allPages) {
print(lastPage.runtimeType) // InfiniteData<int, PageResponse<Post>>,
print(allPages.runtimeType) // List<InfiniteData<int, PageResponse<Post>>>,
return lastPage.data.nextPage;
},
);
copied to clipboard
Mutation #
// MutationResult<Post, Error, void Function(String), Future<Post> Function(String)>
late final createPost = Mutation.use<String, Post, Error>(
mutationFn: (String content) => repository.createPost(content),
onMutate: (param) => print('mutate started'),
onSuccess: (param, data) => print('mutate succeed'),
onError: (param, error) => print('mutate error occurred'),
);
createPost.mutate('some content');
// or
await createPost.mutateAsync('some content');
copied to clipboard
Mutation without parameters #
Sometimes you may need a Mutation without parameters. In such situations, you can use the Mutation.noParams constructor.
// MutationResult<Post, Error, void Function(), Future<void> Function()>
late final createRandomPost = Mutation.noParams<Post, Error>(
mutationFn: () => repository.createRandomPost(),
onMutate: () => print('mutate started'),
onSuccess: (data) => print('mutate succeed'),
onError: (error) => print('mutate error occurred'),
);
createRandomPost.mutate();
// or
await createRandomPost.mutateAsync();
copied to clipboard
Fuery Client #
// invalidate.
Fuery.invalidateQueries(queryKey: ['posts']);
// Default query options configuration
Fuery.configQueryOptions(
query: QueryOptions(...),
infiniteQuery: InfiniteQueryOptions(...),
);
copied to clipboard
Todo #
More complex features like query-core (from react-query)
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.