felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.8k stars 3.39k forks source link

Yield new state manually #845

Closed hawkinsjb1 closed 4 years ago

hawkinsjb1 commented 4 years ago

Sometimes I find myself using states to trigger a certain listener, and once the listener has been triggered I would like to go back to an 'Idle' state. I know I can create empty events that meet this purpose, but I was wondering if we could do this inline from a BlocProvider.of(context) reference. For example:

listener: (context, state) {
  if (state is AddedItem) {
    // do something
    BlocProvider.of(context).yield(InitialState())
  }
}

I realize this may go against the BLoC pattern, but it would also reduce unnecessary boilerplate

tenhobi commented 4 years ago

Hi, could you provide more context?

tenhobi commented 4 years ago

This goes against the very basics of architecture an app. With BLoC or any other state management solution, you are trying to separate your code to layers - view and logic. Try rather architecture-out your problem, so you would keep your code nice and clean. 💙

felangel commented 4 years ago

Hi @hawkinsjb1 👋 Thanks for opening an issue!

As @tenhobi mentioned this violates the bloc library paradigm. I think in this case, the problem is it seems you're thinking of states as triggers when the events should be the triggers.

Rather than having an AddedObject state I would recommend having an AddedObject event which triggers an update in the bloc's state. Hope that helps!

felangel commented 4 years ago

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 👍