hydrate method
void
hydrate({
- Storage? storage,
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({Storage? storage}) {
__storage = 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;
}
}