delegate property
Implementation
@meta.visibleForTesting
@override
Map<K, T> get delegate {
try {
final fileContents = file.readAsStringSync();
final configMap =
const convert.JsonDecoder().convert(fileContents) as Map;
return {
...defaults,
...Map<K, T>.from(configMap),
};
} on io.FileSystemException catch (exception) {
// ENOENT - file or directory does not exist.
// We don't create until a value is set.
if (exception.osError?.errorCode == 2 ||
exception.osError?.errorCode == 3) {
return {
...defaults,
};
}
// EACCES - no permission to access the file.
// Throw a nicer error message.
if (exception.osError?.errorCode == 13) {
throw Exception(
'Access denied when attempting to read the "$id" storagebox at path "${file.path}".',
);
}
rethrow;
} on FormatException {
// The file is empty or contains invalid JSON.
// Next time the map is modified the file will be reset.
return {
...defaults,
};
} catch (e) {
rethrow;
}
}
Implementation
@meta.visibleForTesting
@override
set delegate(Map<K, T> updatedMap) {
try {
final updatedConfigMapString =
const convert.JsonEncoder.withIndent('\t').convert(updatedMap);
file.createSync(recursive: true);
file.writeAsStringSync(
updatedConfigMapString,
flush: true,
);
return;
} on io.FileSystemException catch (exception) {
// EACCES - no permission to access the file.
// Throw a helpful error message.
if (exception.osError?.errorCode == 13) {
throw StorageBoxException(
'Access denied when attempting to write to the "$id" storagebox at path "${file.path}".',
);
}
rethrow;
// ignore: avoid_catching_errors
} on convert.JsonUnsupportedObjectError catch (error) {
final key = updatedMap.keys
.firstWhere((key) => updatedMap[key] == error.unsupportedObject);
throw StorageBoxException(
'Value for key "$key" for "$id" storagebox at path "${file.path}" is not a JSON serializable value.',
);
} catch (e) {
rethrow;
}
}