You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The lack of capacity to fire events from Blocs and Cubits makes us having to create and manage what we usually call "temporary states", which is a kind of a "hack" (cause an event is an event, like 'FetchErrorEvent' for example while the state of a Bloc is, at least for me, another usage).
Desired Solution
I usually implement in my projects this kind of Blocs and Cubits. Example:
class CounterCubit extends EventedCubit<int, CounterEvent> {
CounterCubit() : super(0);
void increment() {
emit(state + 1);
fireEvent(const CounterEvent.incremented()); // <- Here the Cubit fires an "IncrementedEvent"
}
void decrement() {
emit(state - 1);
fireEvent(const CounterEvent.decremented()); // <- Here the Cubit fires a "DecrementedEvent"
}
}
My implementation of EventedCubit, is as follow:
/// {@template evented_cubit}
/// Specialized [Bloc] which handles initializing the [Cubit]
/// stream of events.
/// {@endtemplate}
abstract class EventedCubit<State, Event> extends Cubit<State>
with EventedMixin<Event, State> {
/// {@macro evented_cubit}
EventedCubit(super.state) {
initEvented();
}
}
I use a mixin on "BlocBase<State>" so that I can put there all the implementation code (cause I cannot modify your "bloc" stacks!!)
For the app I created a simple Widget which I call "BlocEventListener" (100% similar to your BlocListener):
Description
The lack of capacity to fire events from Blocs and Cubits makes us having to create and manage what we usually call "temporary states", which is a kind of a "hack" (cause an event is an event, like 'FetchErrorEvent' for example while the state of a Bloc is, at least for me, another usage).
Desired Solution
I usually implement in my projects this kind of Blocs and Cubits. Example:
My implementation of EventedCubit, is as follow:
For the app I created a simple Widget which I call "BlocEventListener" (100% similar to your BlocListener):
Additional Context
What I totally avoid in my apps, is the use of "temporary states" and hacks with "noOps", as shown in the following article:
https://itnext.io/flutter-blocs-at-scale-1-the-state-machine-fce5f086d7b9
The text was updated successfully, but these errors were encountered: