HydratedBloc<Event, State> constructor

HydratedBloc<Event, State>(
  1. State state
)

Specialized Bloc which handles initializing the Bloc state based on the persisted state. This allows state to be persisted across hot restarts as well as complete app restarts.

abstract class CounterEvent {}
class CounterIncrementPressed extends CounterEvent {}
class CounterDecrementPressed extends CounterEvent {}

class CounterBloc extends HydratedBloc<CounterEvent, int> {
  CounterBloc() : super(0) {
    on<CounterIncrementPressed>((event, emit) => emit(state + 1));
    on<CounterDecrementPressed>((event, emit) => emit(state - 1));
  }

  @override
  int fromJson(Map<String, dynamic> json) => json['value'] as int;

  @override
  Map<String, int> toJson(int state) => {'value': state};
}

Implementation

HydratedBloc(State state) : super(state) {
  hydrate();
}