As Easy as a Sync Variable
Manage server state, REST APIs, GraphQL, SQLite, or any Future effortlessly. Just provide a fetcher function and fquery handles state, caching, and lifecycle.
Effortlessly fetch, cache, and update asynchronous data in Flutter. As easy as instantiating a sync variable.
Simply provide your query key and fetch functionβfquery handles caching, loading states, background refetching, deduplication, and garbage collection:
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),
),
);
},
);
}
}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),
),
);
}
}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.
| Feature | Traditional Flutter Approaches | With fquery |
|---|---|---|
| Boilerplate | Tedious isLoading, hasError, data logic across every screen | Zero boilerplate β fully managed reactive state |
| Caching | Manual singletons, SQLite caches, or custom state machines | Automatic in-memory cache with configurable stale time |
| Duplicate Requests | Multiple widgets simultaneously request the same API endpoint | Automatic deduplication into a single in-flight network call |
| Screen Transitions | White loading screens or spinners on every page revisit | Stale-while-revalidate β instant display from cache |
| Memory Management | High risk of memory leaks from stale stream subscriptions | Reference-counted GC automatically purges unused queries |
| Optimistic UI | Complex rollback logic and manual snapshot coordination | Native onMutate rollback context built into every mutation |
| Infinite Lists | Fragile scroll listeners, manual page index math | First-class InfiniteQueryBuilder with bi-directional pagination |
fquery to your project βAdd the dependency to your pubspec.yaml:
flutter pub add fqueryCacheProvider βPlace CacheProvider near the root of your widget tree:
import 'package:flutter/material.dart';
import 'package:fquery/fquery.dart';
final queryCache = QueryCache();
void main() {
runApp(
CacheProvider(
cache: queryCache,
child: const MyApp(),
),
);
}Use QueryBuilder to reactively render your asynchronous data:
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:
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}!');
}
}