Skip to content
fquery banner

Async State Management for Flutter

Effortlessly fetch, cache, and update asynchronous data in Flutter. As easy as instantiating a sync variable.

Pub VersionPub PointsPub DownloadsGitHub Repo starsDiscord Server

Declarative Data Fetching in Action ​

Simply provide your query key and fetch functionβ€”fquery handles caching, loading states, background refetching, deduplication, and garbage collection:

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

class TodosWidget extends StatelessWidget {
  const TodosWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return QueryBuilder<List<Todo>, Exception>(
      options: QueryOptions(
        queryKey: QueryKey(['todos']),
        queryFn: todosAPI.getAll,
        staleDuration: const Duration(seconds: 30),
      ),
      builder: (context, todos) {
        if (todos.isLoading) {
          return const Center(child: CircularProgressIndicator());
        }

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

        return ListView.builder(
          itemCount: todos.data!.length,
          itemBuilder: (context, index) => ListTile(
            title: Text(todos.data![index].title),
          ),
        );
      },
    );
  }
}
dart
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:fquery/fquery.dart';

class TodosPage extends HookWidget {
  const TodosPage({super.key});

  @override
  Widget build(BuildContext context) {
    final todos = useQuery<List<Todo>, Exception>(
      ['todos'],
      todosAPI.getAll,
      staleDuration: const Duration(seconds: 30),
    );

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

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

    return ListView.builder(
      itemCount: todos.data!.length,
      itemBuilder: (context, index) => ListTile(
        title: Text(todos.data![index].title),
      ),
    );
  }
}

Why Choose fquery? ​

In Flutter, managing asynchronous data often leads to boilerplates of FutureBuilder, custom state machines, complex store setups, and fragile manual cache stores. fquery changes that entirely.

FeatureTraditional Flutter ApproachesWith fquery
BoilerplateTedious isLoading, hasError, data logic across every screenZero boilerplate β€” fully managed reactive state
CachingManual singletons, SQLite caches, or custom state machinesAutomatic in-memory cache with configurable stale time
Duplicate RequestsMultiple widgets simultaneously request the same API endpointAutomatic deduplication into a single in-flight network call
Screen TransitionsWhite loading screens or spinners on every page revisitStale-while-revalidate β€” instant display from cache
Memory ManagementHigh risk of memory leaks from stale stream subscriptionsReference-counted GC automatically purges unused queries
Optimistic UIComplex rollback logic and manual snapshot coordinationNative onMutate rollback context built into every mutation
Infinite ListsFragile scroll listeners, manual page index mathFirst-class InfiniteQueryBuilder with bi-directional pagination

Quickstart in 3 Steps ​

1. Add fquery to your project ​

Add the dependency to your pubspec.yaml:

bash
flutter pub add fquery

2. Wrap your application with CacheProvider ​

Place CacheProvider near the root of your widget tree:

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

final queryCache = QueryCache();

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

3. Fetch and display data anywhere ​

Use QueryBuilder to reactively render your asynchronous data:

dart
class ProfilePage extends StatelessWidget {
  const ProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    return QueryBuilder<User, Exception>(
      options: QueryOptions(
        queryKey: QueryKey(['user', 'me']),
        queryFn: api.getProfile,
      ),
      builder: (context, query) {
        if (query.isLoading) {
          return const Center(child: CircularProgressIndicator());
        }

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

        return Text('Welcome, ${query.data!.name}!');
      },
    );
  }
}

If you use flutter_hooks, you can also use useQuery:

dart
class ProfilePage extends HookWidget {
  const ProfilePage({super.key});

  @override
  Widget build(BuildContext context) {
    final user = useQuery(['user', 'me'], api.getProfile);

    if (user.isLoading) return const CircularProgressIndicator();
    if (user.isError) return Text('Error: ${user.error}');

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

Released under the MIT License.