loadConverting method
This method can be used if you were using a Json sequence file with a ".db" termination, and wants to convert it to a regular Json file. This only works if your original ".db" file has a single object.
- It first loads a Json file called "dbName.json".
- If the file exists and is NOT empty, return its content as a single simple object.
- If the file exists and is empty, returns null.
- If the file doesn't exist, goes to step 2.
- Next, tries loading a Json-SEQUENCE file called "dbName.db".
- If the file doesn't exist, returns null.
- If the file exists and is empty, saves it as an empty Json file called "dbName.json"
- If the file exists with a single object, saves it as a Json file called "dbName.json"
- If the file exists and has 2 or more objects:
- If
isList
is false, throws an exception. - If
isList
is true, wraps the result in a List
- If
Implementation
Future<Object?> loadConverting({required bool isList}) async {
//
_checkIfFileSystemIsTheSame();
File file = _file ?? await this.file();
if (!file.existsSync())
return _readsFromJsonSequenceDbFile(isList);
else {
Uint8List encoded;
try {
// Loads the '.json' (Json) file.
encoded = await file.readAsBytes();
} catch (error) {
if ((error is FileSystemException) && //
error.message.contains("No such file or directory"))
return _readsFromJsonSequenceDbFile(isList);
rethrow;
}
Object? simpleObjs = decodeJson(encoded);
return simpleObjs;
}
}