fuery 0.0.1 copy "fuery: ^0.0.1" to clipboard
fuery: ^0.0.1 copied to clipboard

Asynchronous state management library that helps implements the server side state management in Flutter

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

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) {
            ...
        }
    }
);

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) {
            ...
        }
    }
);

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');

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();

MutationBuilder #


// Shows loading barrier when deleting todo item.
MutationBuilder(
  mutation: deleteTodo,
  builder: (context, state) {
    if (state.status.isPending) {
      return LoadingBarrier();
    }

    return const SizedBox();
  },
),

Fuery Client #

// invalidate.
Fuery.invalidateQueries(queryKey: ['posts']);

// Default query options configuration
Fuery.configQueryOptions(
  query: QueryOptions(...),
  infiniteQuery: InfiniteQueryOptions(...),
);

Todo #

  • More complex features like query-core (from react-query)
2
likes
150
points
25
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Asynchronous state management library that helps implements the server side state management in Flutter

Homepage
Repository (GitHub)
View/report issues

Topics

#react-query #state-management #server-state

License

MIT (license)

Dependencies

flutter, fuery_core

More

Packages that depend on fuery