Bloc Observer
- flutter_bloc: 7.0.0
 
Cubit
BlocObserver
class CubitTestObserver extends BlocObserver {
  
  void onCreate(BlocBase bloc) {
    super.onCreate(bloc);
    print('onCreate -- ${bloc.runtimeType}');
  }
  
  void onChange(BlocBase bloc, Change change) {
    super.onChange(bloc, change);
    print('onChange -- ${bloc.runtimeType}, $change');
  }
  
  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
    print('onError -- ${bloc.runtimeType}, $error');
    super.onError(bloc, error, stackTrace);
  }
  
  void onClose(BlocBase bloc) {
    super.onClose(bloc);
    print('onClose -- ${bloc.runtimeType}');
  }
}
void main() {
    Bloc.observer = CubitTestObserver();
}
BlocObserver를 상속하고 필요한 함수만 오버라이드하여 수정한 후 Bloc.observer에 등록하면 모든 Cubit의 변화를 확인 할 수 있습니다.
emit(State) -> onChange 순으로 호출됩니다.
Bloc
BlocObserver
class BlocTestObserver extends BlocObserver {
  
  void onCreate(BlocBase bloc) {
    super.onCreate(bloc);
    print('onCreate -- ${bloc.runtimeType}');
  }
  
  void onEvent(Bloc bloc, Object? event) {
    super.onEvent(bloc, event);
    print('onEvent -- ${bloc.runtimeType}, $event');
  }
  
  void onChange(BlocBase bloc, Change change) {
    super.onChange(bloc, change);
    print('onChange -- ${bloc.runtimeType}, $change');
  }
  
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print('onTransition -- ${bloc.runtimeType}, $transition');
  }
  
  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
    print('onError -- ${bloc.runtimeType}, $error');
    super.onError(bloc, error, stackTrace);
  }
  
  void onClose(BlocBase bloc) {
    super.onClose(bloc);
    print('onClose -- ${bloc.runtimeType}');
  }
}
void main() {
    Bloc.observer = BlocTestObserver();
}
BlocObserver를 상속하고 필요한 함수만 오버라이드하여 수정한 후 Bloc.observer에 등록하면 모든 Bloc의 변화를 확인 할 수 있습니다. Bloc는 추가적으로 onEvent와 onTransition 함수를 호출 할 수 있습니다.
add(Event) -> onEvent -> yield State -> onChange and onTransition 순으로 호출됩니다.