Bloc: BlocObserver Duplicate Call

Created on 27 Jul 2020  路  2Comments  路  Source: felangel/bloc

Hi @felangel ,

I found if we override onTransition and onChange in BlocObserver(If we apply both cubit and bloc in the app), it will call onTransition and then call onChange. In my case, the console will print transition info and then print change info, which is highly duplicate.

I understood bloc now is built on the cubit, so the onChange should be called, but is it possible when the onTransition is called, the onChange will not be called again?

question

Most helpful comment

Hi @Peng-Qian 馃憢
Thanks for opening an issue!

This is intentional because it would be very strange for onChange to not function for blocs and it is also used by other packages like hydrated_bloc to ensure compatibility between the two. If you don't want onChange to fire for blocs you can just update your BlocObserver:

class MyBlocObserver extends BlocObserver {
  void onChange(Cubit cubit, Change change) {
    if (cubit is! Bloc) print(change);
    super.onChange(cubit, change);
  }
}

Hope that helps 馃憤

All 2 comments

Hi @Peng-Qian 馃憢
Thanks for opening an issue!

This is intentional because it would be very strange for onChange to not function for blocs and it is also used by other packages like hydrated_bloc to ensure compatibility between the two. If you don't want onChange to fire for blocs you can just update your BlocObserver:

class MyBlocObserver extends BlocObserver {
  void onChange(Cubit cubit, Change change) {
    if (cubit is! Bloc) print(change);
    super.onChange(cubit, change);
  }
}

Hope that helps 馃憤

Many thx!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Reidond picture Reidond  路  3Comments

rsnider19 picture rsnider19  路  3Comments

zjjt picture zjjt  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

RobPFarley picture RobPFarley  路  3Comments