reversible_bloc 0.0.1 reversible_bloc: ^0.0.1 copied to clipboard
A bloc mixin that enables to rollback states.
A Bloc mixin that enables to revert the bloc / cubit to a previous state.
Usage #
To use reversible_bloc, you need to import and mix ReversibleBlocMixin
to your Bloc
or Cubit
.
class MyReversibleCubit extends Cubit<int> with ReversibleBlocMixin {
MyReversibleCubit(int initialState) : super(initialState);
void changeValue(int newValue) => emit(newValue);
}
Then, when you need to rollback to previous state, you can use revert()
method.
final myReversibleCubit = context.read<MyReversibleCubit>();
myReversibleCubit.changeValue(2);
myReversibleCubit.changeValue(4);
// Current state is 4
myReversibleCubit.revert();
// Current state back to 2
myReversibleCubit.revert();
// Current state back to initial state
myReversibleCubit.revert();
// throws EmptyStackException
How to contribute #
Feel free to add sugestions and report bugs in Issues page. PRs are welcomed too.