ex_bloc 0.3.0
ExBloc provide simple way to organize bloc middleware and dispatch events.
ex_bloc
ExBloc provide simple way to organize bloc middleware and dispatch events.
Usage
Import
import 'package:ex_bloc/ex_bloc.dart';
Get ExStore
final store = ExStore.instance;
Simple state
class TestState {
final bool afterInitial;
const TestState({this.afterInitial});
factory TestState.initial() {
return const TestState(afterInitial: false);
}
}
Simple bloc
ExBloc does not contain business logic.
class TestBloc extends ExBloc<TestEvent, TestState> {
@override
TestState get initialState => TestState.initial();
}
Event
ExEvent provide business logic and payload
ExStore provide ability to dispatch other events or get other states.
class TestEvent extends ExEvent<TestState, TestBloc> {
@override
Stream<TestState> call(ExStore store, TestBloc bloc) async* {
yield TestState(afterInitial: true);
}
}
Dispatch events
store.dispatch(TestEvent());
or
TestEvent().dispatch();
Get state from ExStore
final store = ExStore.instance;
final testState = store.getState<TestState>();
Subscribe event
final _subscribeSpecific = ExStore.instance.on<TestEvent>().listen((e) => print('do something'));
final _subscribeAny = ExStore.instance.on().listen((e) => print('do something'));
_subscribeSpecific.cancel();
_subscribeAny.cancel();
Wait for event
await ExStore.instance.wait<TestEvent>();
Reselect usage
Reselect can be used if needed
final testStateSelector = (ExStore store) => store.getState<TestState>();
final afterInitialSelector = createSelector1(
testStateSelector,
(TestState testState) => testState.afterInitial,
);