Bloc: Allow user to force emit a new state

Created on 8 Nov 2020  路  6Comments  路  Source: felangel/bloc

Is your feature request related to a problem? Please describe.
A new state will not be emitted if the previous_state == new_state. While this is usually the desired behavior. This doesn't work when I am using the super_enum package (https://pub.dev/packages/super_enum) to represent my different states. Adding this option doesn't do too much harm and it gives the user mode control of the behavior of the cubit.

Describe the solution you'd like
Allow force emitting a state through an optional parameter in the emit method.

question

All 6 comments

Hi @jinyus 馃憢
Thanks so much for opening an issue!

Can you please provide a link to a sample app which illustrates the issue you鈥檙e facing? Thanks 馃檹

I am making the example code and I want to know when emit has fired...does the onChange in BlocObserver indicates when a state has been emitted?

Yup onChange is called when a new state is emitted 馃憤

Hi @jinyus this is because you are mutating the state rather than emitting a new instance.

I modified

var currentMessages = s.messages;

to

final currentMessages = List.of(s.messages);

and that resulted in:

$ dart lib/main.dart
lib/main.dart: Warning: Interpreting this as package URI, 'package:cubit_superenum_sample/main.dart'.
onChange -- cubit: LoggerCubit, change: Change { currentState: Initial(), nextState: Loaded(messages: [hello 1]) }
onChange -- cubit: LoggerCubit, change: Change { currentState: Loaded(messages: [hello 1]), nextState: Loaded(messages: [hello 1, hello 2]) }
onChange -- cubit: LoggerCubit, change: Change { currentState: Loaded(messages: [hello 1, hello 2]), nextState: Loaded(messages: [hello 1, hello 2, hello 3]) }
onChange -- cubit: LoggerCubit, change: Change { currentState: Loaded(messages: [hello 1, hello 2, hello 3]), nextState: Loaded(messages: [hello 1, hello 2, hello 3, hello 4]) }

Hope that helps 馃憤

Thanks @felangel , works as expected now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krusek picture krusek  路  3Comments

wheel1992 picture wheel1992  路  3Comments

komapeb picture komapeb  路  3Comments

frankrod picture frankrod  路  3Comments

RobPFarley picture RobPFarley  路  3Comments