Skip to content

Setup & Provider

The FasqBlocProvider is the entry point for using Fasq with Bloc. It injects the QueryClient into your widget tree, making it accessible to all your Cubits and Widgets.

Wrap your root widget (usually MaterialApp) with FasqBlocProvider. This automatically creates and manages a QueryClient for you.

import 'package:flutter/material.dart';
import 'package:fasq_bloc/fasq_bloc.dart';
void main() {
runApp(
FasqBlocProvider(
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}

If you need to configure the QueryClient (e.g., to set global cache times or base URLs), you can create it yourself and pass it to the provider.

void main() {
// 1. Create a custom client
final queryClient = QueryClient(
defaultOptions: DefaultOptions(
queries: QueryOptions(
staleTime: Duration(minutes: 5), // Cache data for 5 minutes
),
),
);
runApp(
FasqBlocProvider(
// 2. Inject your custom client
client: queryClient,
child: const MyApp(),
),
);
}

You can access the QueryClient anywhere in your widget tree using context.read or the static of method.

class SettingsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
// Access via extension (Requires provider/flutter_bloc)
final client = context.read<QueryClient>();
client.invalidateQueries();
},
child: Text('Clear Cache'),
);
}
}

When using QueryCubit or MutationCubit, the client is automatically injected for you if you don’t provide one.

// The client is automatically resolved from context when this Cubit is created
class UserCubit extends QueryCubit<User> {
// ...
}