tryFetchAndDeserializeJsonFile<T> static method
Tries to fetch and deserialize a json file from the local storage. If the file does not exist null will be returned.
fileName The name of the file to fetch.
localStoragePath The path to the local storage directory.
fromJson A function that is used to deserialize the json data.
Throws a ReadException if an error occurs during file reading. Throws a DeserializationException if an error occurs during deserialization.
Implementation
static Future<T?> tryFetchAndDeserializeJsonFile<T>({
required final String fileName,
required final String localStoragePath,
required final T Function(Map<String, dynamic> json) fromJson,
}) async {
final file = File(p.join(localStoragePath, fileName));
if (!file.existsSync()) return null;
String fileContent;
try {
fileContent = file.readAsStringSync();
} catch (e, stackTrace) {
throw ReadException(file, e, stackTrace);
}
try {
return fromJson(jsonDecode(fileContent) as Map<String, dynamic>);
} catch (e, stackTrace) {
throw DeserializationException(file, e, stackTrace);
}
}