reversible_bloc 0.0.2 reversible_bloc: ^0.0.2 copied to clipboard
A bloc mixin that enables to rollback states.
reversible_bloc #
A Bloc mixin that enables to revert the bloc / cubit to a previous state.
Setup #
Add dependency to your pubspec.yaml
dependencies:
reversible_bloc : ^0.0.2
Run pub get
to install.
Usage #
To use reversible_bloc, you need to import and mix ReversibleBlocMixin
to your Bloc
or Cubit
.
import 'package:reversible_bloc/reversible_bloc.dart';
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. If bloc has no previous state, it will throw an EmptyStackException
.
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.