Last updated:
0 purchases
fuery
Fuery Core #
Asynchronous state management library that helps implements the server side state management in Flutter #
Features #
Async data fetching, caching, invalidation, pagination
Mutation with side effect
Widget builders and listeners
Installation #
flutter pub add fuery
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),
);
// Widget
...
return QueryBuilder(
query: post,
builder: (context, state) {
if (state.status.isPending) {
...
}
if (state.status.isError) {
...
}
}
);
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;
},
);
// Widget
...
return InfiniteQueryBuilder(
query: posts,
builder: (context, state) {
if (state.status.isPending) {
...
}
if (state.status.isError) {
...
}
}
);
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
MutationBuilder #
// Shows loading barrier when deleting todo item.
MutationBuilder(
mutation: deleteTodo,
builder: (context, state) {
if (state.status.isPending) {
return LoadingBarrier();
}
return const SizedBox();
},
),
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.