Sub topic 15
Logging every change with BlocObserver
This chapter assumes you know Dart classes, Flutter widget lifecycles, and the basic flutter_bloc APIs. A BlocObserver is a global hook for observing the lifecycle of every Cubit and Bloc in an application. It helps you understand when state changes, events are added, errors occur, and instances are closed.
Why use a global observer?
Logging inside every Cubit or Bloc duplicates code and makes debugging inconsistent. A single observer gives you one place to inspect state-management activity.
A global observer can report:
onCreate: a Cubit or Bloc is constructed.onChange: a Cubit or Bloc emits a new state.onEvent: a Bloc receives an event.onTransition: a Bloc processes an event and produces a state transition.onError: an uncaught error reaches the Bloc library.onClose: a Cubit or Bloc is closed.
A Cubit has changes but no events or transitions. A Bloc has all of these because it receives events before producing states.
Implementing an observer
Extend BlocObserver and override only the callbacks that matter to your diagnostics. Register the observer before calling runApp.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class AppBlocObserver extends BlocObserver {
@override
void onCreate(BlocBase<Object?> bloc) {
super.onCreate(bloc);
debugPrint('created ${bloc.runtimeType}');
}
@override
void onChange(BlocBase<Object?> bloc, Change<Object?> change) {
super.onChange(bloc, change);
debugPrint('${bloc.runtimeType}: $change');
}
@override
void onEvent(Bloc<Object?> bloc, Object? event) {
super.onEvent(bloc, event);
debugPrint('${bloc.runtimeType} received $event');
}
@override
void onError(
BlocBase<Object?> bloc,
Object error,
StackTrace stackTrace,
) {
debugPrint('${bloc.runtimeType} failed: $error');
super.onError(bloc, error, stackTrace);
}
@override
void onClose(BlocBase<Object?> bloc) {
debugPrint('closed ${bloc.runtimeType}');
super.onClose(bloc);
}
}
void main() {
Bloc.observer = AppBlocObserver();
runApp(const MaterialApp(home: Scaffold()));
}Calling super preserves the library's default behavior. Keep those calls unless you have a specific reason to omit them.
Understanding the event flow
For a Bloc, the useful debugging sequence is event, transition, and change. The transition includes the event, previous state, and next state. The change describes the previous and next states after emission.
Logging safely
State objects and events may contain personal data, access tokens, or large collections. Avoid logging them verbatim in production. Use identifiers, counts, and status values instead. You can also register a detailed observer only in debug builds.
An observer reports activity. It does not replace error handling, tests, or user-facing failure states. Use it to locate a failure, then handle that failure in the relevant Bloc or Cubit.
Exercise
Which callback should you inspect first when a Bloc appears to receive an event but the UI does not update?
Show answer
Inspect onEvent, onTransition, and onChange in that order. This shows whether the event arrived, whether it produced a transition, and whether a new state was emitted.