QueryObserver class abstract
A BlocObserver specialised for query and mutation controllers.
Since QueryController, InfiniteQueryController, and
MutationController all extend Cubit, the standard BlocObserver
already fires for them automatically. QueryObserver filters those
events and re-exposes them as typed, cache-key–aware hooks so you
don't have to downcast BlocBase in your own observer.
Registration
Register via QueryClient — no direct flutter_bloc dependency needed:
// main.dart
QueryClient.instance.setObserver(AppQueryObserver());
Usage
class AppQueryObserver extends QueryObserver {
@override
void onQueryCreate(String cacheKey) =>
debugPrint('Controller created: $cacheKey');
@override
void onQueryChange(
String cacheKey,
QueryState<dynamic> currentState,
QueryState<dynamic> nextState,
) {
debugPrint('[$cacheKey] ${currentState.status} → ${nextState.status}');
}
@override
void onQueryError(String cacheKey, Object error, StackTrace stackTrace) =>
FirebaseCrashlytics.instance.recordError(error, stackTrace);
@override
void onQueryClose(String cacheKey) =>
debugPrint('Controller closed: $cacheKey');
}
Non-query cubits
You can still handle non-query cubits by overriding the raw
BlocObserver hooks. Always call super to preserve query-specific
behaviour:
@override
void onChange(BlocBase<dynamic> bloc, Change<dynamic> change) {
super.onChange(bloc, change); // ← fires onQueryChange for query controllers
if (bloc is MyOtherCubit) { ... }
}
Constructors
- QueryObserver()
-
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onChange(
BlocBase bloc, Change change) → void -
Called whenever a
Changeoccurs in anyblocAchangeoccurs when a new state is emitted. onChange is called before a bloc's state has been updated. -
onClose(
BlocBase bloc) → void -
Called whenever a
Blocis closed. onClose is called just before theBlocis closed and indicates that the particular instance will no longer emit new states. -
onCreate(
BlocBase bloc) → void -
Called whenever a
Blocis instantiated. In many cases, a cubit may be lazily instantiated and onCreate can be used to observe exactly when the cubit instance is created. -
onDone(
Bloc bloc, Object? event, [Object? error, StackTrace? stackTrace]) → void -
Called whenever an
eventhandler for a specificblochas completed. This may include anerrorandstackTraceif an uncaught exception occurred within the event handler.inherited -
onError(
BlocBase bloc, Object error, StackTrace stackTrace) → void -
Called whenever an
erroris thrown in anyBlocorCubit. ThestackTraceargument may be StackTrace.empty if an error was received without a stack trace. -
onEvent(
Bloc bloc, Object? event) → void -
Called whenever an
eventisaddedto anyblocwith the givenblocandevent.inherited -
onQueryChange(
String cacheKey, QueryState currentState, QueryState nextState) → void - Called on every state change for a query or mutation controller.
-
onQueryClose(
String cacheKey) → void - Called when a QueryController, InfiniteQueryController, or MutationController is closed.
-
onQueryCreate(
String cacheKey) → void - Called when a QueryController, InfiniteQueryController, or MutationController is first created.
-
onQueryError(
String cacheKey, Object error, StackTrace stackTrace) → void - Called when a query or mutation controller encounters an unhandled stream error.
-
onTransition(
Bloc bloc, Transition transition) → void -
Called whenever a transition occurs in any
blocwith the givenblocandtransition. Atransitionoccurs when a neweventis added and a new state isemittedfrom a correspondingEventHandler.onTransitionis called before abloc's state has been updated.inherited -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited