hydrate method
void
hydrate()
inherited
Populates the internal state storage with the latest state.
This should be called when using the HydratedMixin
directly within the constructor body.
class CounterBloc extends Bloc<CounterEvent, int> with HydratedMixin {
CounterBloc() : super(0) {
hydrate();
}
...
}
Implementation
void hydrate() {
final storage = HydratedBloc.storage;
try {
final stateJson = storage.read(storageToken) as Map<dynamic, dynamic>?;
_state = stateJson != null ? _fromJson(stateJson) : super.state;
} catch (error, stackTrace) {
onError(error, stackTrace);
_state = super.state;
}
try {
final stateJson = _toJson(state);
if (stateJson != null) {
storage.write(storageToken, stateJson).then((_) {}, onError: onError);
}
} catch (error, stackTrace) {
onError(error, stackTrace);
if (error is StorageNotFound) rethrow;
}
}