init method
Implementation
void init(ErrorHandler errorHandler, bool storageJson) {
this._errorHandler = errorHandler;
addListener((state) {
if (_persistance != null) {
_hasUpdated = true;
} else {
_persistance = storageJson
? _store.setPersisted(_key, toJson(state)).whenComplete(_whenPersistenceComplete)
: null;
}
});
_store.ready.then<void>((_) async {
Map<String, dynamic>? rawV;
try {
rawV = await _store.getPersisted(_key);
} on FormatException catch (e) {
// File corruption should be less likely with removal of async code in writes
// Existing corrupted files are cleaned up here without failing initialization
_store.setPersisted(_key, {});
log("Clean file $_key with format error", kind: LogFilterKind.warning);
final wrappedError = ErrorLoadingStorage(e);
errorHandler(wrappedError);
}
T v;
if (rawV == null) {
final init = await _initialiser();
_persistance = storageJson
? _store.setPersisted(_key, toJson(init)).whenComplete(_whenPersistenceComplete)
: null;
_notifier.nonNullState = init;
v = init;
} else {
v = fromJson(rawV);
_notifier.nonNullState = v;
}
_isReady = true;
if (_getCompleter != null) {
_getCompleter!.complete(v);
}
_readyCompleter.complete();
return;
}).catchError((e) {
_error = e;
final wrappedError = ErrorLoadingStorage(e);
errorHandler(wrappedError);
throw wrappedError;
});
}