Skip to main content

Bloc Observer

  • flutter_bloc: 7.0.0

Cubit#

BlocObserver#

class CubitTestObserver extends BlocObserver {
@override
void onCreate(BlocBase bloc) {
super.onCreate(bloc);
print('onCreate -- ${bloc.runtimeType}');
}
@override
void onChange(BlocBase bloc, Change change) {
super.onChange(bloc, change);
print('onChange -- ${bloc.runtimeType}, $change');
}
@override
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
print('onError -- ${bloc.runtimeType}, $error');
super.onError(bloc, error, stackTrace);
}
@override
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 {
@override
void onCreate(BlocBase bloc) {
super.onCreate(bloc);
print('onCreate -- ${bloc.runtimeType}');
}
@override
void onEvent(Bloc bloc, Object? event) {
super.onEvent(bloc, event);
print('onEvent -- ${bloc.runtimeType}, $event');
}
@override
void onChange(BlocBase bloc, Change change) {
super.onChange(bloc, change);
print('onChange -- ${bloc.runtimeType}, $change');
}
@override
void onTransition(Bloc bloc, Transition transition) {
super.onTransition(bloc, transition);
print('onTransition -- ${bloc.runtimeType}, $transition');
}
@override
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
print('onError -- ${bloc.runtimeType}, $error');
super.onError(bloc, error, stackTrace);
}
@override
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 순으둜 ν˜ΈμΆœλ©λ‹ˆλ‹€.

Last updated on