Skip to content

Flutter Integration (fquery) ​

fquery is the official Flutter integration for fquery_core. It wraps the pure Dart query cache and observers into reactive Flutter widgets and ready-to-use Flutter Hooks.

bash
flutter pub add fquery

1. Setup: CacheProvider ​

Wrap your widget tree with CacheProvider to provide the QueryCache to all child widgets and hooks:

dart
import 'package:flutter/widgets.dart';
import 'package:fquery/fquery.dart';

final queryCache = QueryCache(
  defaultQueryOptions: DefaultQueryOptions(
    staleDuration: Duration(seconds: 30),
    cacheDuration: Duration(minutes: 5),
  ),
);

void main() {
  runApp(
    CacheProvider(
      cache: queryCache,
      child: const MyApp(),
    ),
  );
}

2. Using with flutter_hooks ​

If your project uses flutter_hooks, fquery provides a first-class, seamless experience matching the ergonomics of TanStack React Query:

dart
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:fquery/fquery.dart';

class UserProfilePage extends HookWidget {
  final int userId;
  const UserProfilePage({super.key, required this.userId});

  @override
  Widget build(BuildContext context) {
    // Automatically subscribes and triggers rebuilds when query updates
    final user = useQuery<User, Exception>(
      ['users', userId],
      () => api.fetchUser(userId),
      staleDuration: const Duration(seconds: 60),
    );

    if (user.isLoading) {
      return const Center(child: CircularProgressIndicator());
    }

    if (user.isError) {
      return Center(child: Text('Error: ${user.error}'));
    }

    return Center(
      child: Text('Welcome, ${user.data?.name}!'),
    );
  }
}

Available Hooks ​

  • useQuery: Observes a single query key.
  • useMutation: Manages a mutation function with reactive state and callbacks (onSuccess, onError).
  • useInfiniteQuery: Manages a paginated list with fetchNextPage and fetchPreviousPage.
  • useQueries: Observes multiple queries in parallel.
  • useIsFetching: Tracks the total count of queries currently fetching across the app.

3. Using with Widgets (QueryBuilder) ​

If you don't use flutter_hooks, fquery includes declarative builder widgets that work anywhere in a standard StatelessWidget or StatefulWidget:

dart
class PostDetailWidget extends StatelessWidget {
  final int postId;
  const PostDetailWidget({super.key, required this.postId});

  @override
  Widget build(BuildContext context) {
    return QueryBuilder<Post, Exception>(
      options: QueryOptions(
        queryKey: QueryKey(['posts', postId]),
        queryFn: () => api.getPost(postId),
      ),
      builder: (context, post) {
        if (post.isLoading) {
          return const Center(child: CircularProgressIndicator());
        }

        if (post.isError) {
          return Center(child: Text('Failed: ${post.error}'));
        }

        return Text(post.data!.title);
      },
    );
  }
}

Available Builders ​

  • QueryBuilder: Declarative single-query builder.
  • MutationBuilder: Handles mutation state and triggers.
  • InfiniteQueryBuilder: Paginated scroll builder.
  • QueriesBuilder: Dynamic parallel queries builder.
  • IsFetchingBuilder: Reactive indicator for network activity.

4. Reading Query State Outside Builders ​

You can read or trigger queries in event listeners (like buttons or scroll controllers) without rebuilds using static accessors:

dart
final userQuery = QueryInstance.of(context, userQueryOptions);
userQuery.refetch();

final feedQuery = InfiniteQueryInstance.of(context, feedQueryOptions);
feedQuery.fetchNextPage();

Released under the MIT License.