LocalJsonPersist class
Save a simple-object in a file, in UTF-8 Json format.
Use save to save as Json:
var persist = LocalJsonPersist("xyz");
var simpleObj = "Hello";
await persist.saveJson(simpleObj);
Use load to load from Json:
var persist = LocalJsonPersist("xyz");
Object? decoded = await persist.loadJson();
Examples of valid JSON includes:
42
42.5
"abc"
1, 2, 3
"42", 123
{"42": 123}
Examples of invalid JSON includes:
4, 5, 6
// Not valid because Json does not allow two separate objects.
1, 2, 3 // Not valid because Json does not allow comma separated objects.
'abc' // Not valid because string must use double quotes.
{42: "123"} // Not valid because a map key must be of type string.
Constructors
-
LocalJsonPersist(Object dbName, {String? dbSubDir, List<
Object> ? subDirs}) -
Saves to
appDocsDir/db/${dbName}.json
- LocalJsonPersist.from(File file)
- Saves to the given file.
Properties
Methods
-
delete(
) → Future< bool> - Deletes the file. If the file was deleted, returns true. If the file did not exist, return false.
-
exists(
) → Future< bool> - Returns true if the file exist. False, otherwise.
-
file(
) → Future< File> - Gets the file.
-
length(
) → Future< int> - Returns the file length. If the file doesn't exist, or exists and is empty, returns 0.
-
load(
) → Future< Object?> - Loads a simple-object from a JSON file. If the file doesn't exist, returns null. A JSON can be a String, a number, null, true, false, '{' (a map) or ']' (a list). Note: The file must contain a single JSON, and it can't be empty. It can, however simple contain 'null' (without the quotes) which will return null.
-
loadAsObj(
) → Future< Map< String, dynamic> ?> - Same as load, but expects the file to be a Map<String, dynamic> representing a single object. Will fail if it's not a map. It may return null.
-
loadAsObjConverting(
) → Future< Map< String, dynamic> ?> - Same as loadConverting, but expects the file to be a Map<String, dynamic> representing a single object. Will fail if it's not a map. It may return null.
-
loadConverting(
{required bool isList}) → Future< Object?> - 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.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
save(
Object? simpleObj) → Future< File> - Saves the given simple object as JSON. If the file exists, it will be overwritten.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- appDocDir → Directory?
-
no setter
- defaultDbSubDir ↔ String
-
The default is saving/loading to/from "appDocsDir/db/".
This is not final, so you can change it.
Make it an empty string to remove it.
getter/setter pair
Static Methods
-
decodeJson(
Uint8List bytes) → Object? -
Decodes a single JSON into a simple object, from the given
bytes
. -
encodeJson(
Object? simpleObj) → Uint8List -
Decodes a single simple object into a JSON, from the given
simpleObj
. -
pathName(
String? dbName, {String? dbSubDir, List< String> ? subDirs}) → String -
resetFileSystem(
) → void -
setFileSystem(
FileSystem fileSystem) → void - You can set a memory file-system in your tests. For example:
-
simpleObjsToString(
List< Object?> ? simpleObjs) → String?
Constants
- jsonTermination → const String
- The default is adding a ".json" termination to the file name.