Bloc: [Doc] SimpleBlocObserver is not a Bloc

Created on 29 Sep 2020  路  2Comments  路  Source: felangel/bloc

Hi !
I think there's a problem with the documentation, specifically the BlocObserver part (https://bloclibrary.dev/#/coreconcepts?id=blocobserver-1).

The problem is, The doc is advising user to call the SimpleBlocObserver as a Bloc in main.dart but If you check the BlocObserver's class which is extended by SimpleBlocObserver, this is not a Bloc. So it cannot work.

What can we do to create a "generic" bloc to logg everything straight out any bloc ?

Thanks !
KBZH

question

Most helpful comment

Thanks for your reply !
Oh yeah, I didn't saw the dot before observer (Bloc . observer) due to the (too much) dark mode.

Sorry !

All 2 comments

Hi @Kobreizhyashi 馃憢

The doc is only advising the user to instantiate the observer in main.dart: Bloc.observer = SimpleBlocObserver();

What can we do to create a "generic" bloc to logg everything straight out any bloc ?

You need to create a class like:

class SimpleBlocObserver extends BlocObserver {
  @override
  void onChange(Cubit cubit, Change change) {
    print('${cubit.runtimeType} $change');
    super.onChange(cubit, change);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    print('${bloc.runtimeType} $transition');
    super.onTransition(bloc, transition);
  }

  @override
  void onError(Cubit cubit, Object error, StackTrace stackTrace) {
    print('${cubit.runtimeType} $error $stackTrace');
    super.onError(cubit, error, stackTrace);
  }
}

and instantiate in main.dart:

void main() {
  Bloc.observer = SimpleBlocObserver();
}

Thanks for your reply !
Oh yeah, I didn't saw the dot before observer (Bloc . observer) due to the (too much) dark mode.

Sorry !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

frankrod picture frankrod  路  3Comments

tigranhov picture tigranhov  路  3Comments

timtraversy picture timtraversy  路  3Comments

rsnider19 picture rsnider19  路  3Comments

Reidond picture Reidond  路  3Comments