SimpleJsonPersistence<T extends HasToJson> class

Simple storage for any objects which can be serialized to json.

Right now for each type (name) it should only be used by one application in one isolate at the same time, otherwise they would overwrite their changes.

Each type has one instance of this persistence class ie. calling SimpleJsonPersistence.getForTypeSync multiple times for the same type will return the same instance. Once load has been called it will be cached/kept in memory forever.

Implementers

Properties

cachedValue → T?
Useful for using as initialValue in StreamBuilder.
no setter
defaultCreator → (T Function()?)
final
fromJson FromJson<T>
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
name String
Name of the store (used as file name for the .json file)
final
onValueChanged Stream<T?>
Stream which will receive a new notification on every save call.
no setter
onValueChangedAndLoad Stream<T?>
Stream with the current value as first event, concatenated with onValueChanged.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
storeBackend StoreBackend
final

Methods

delete() Future<void>
dispose() Future<void>
Removes this store from memory. Probably not really useful in a real world app and should not be used outside of testing.
load() Future<T?>
Loads and deserializes data from storage. It is safe to be called multiple times. Subsequent calls will return the same future. If loaded, the cached value will be returned. (cachedValue). If file does not exist the default value (defaultCreator) will be returned.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onValueChangedOrDefault(Future<T> defaultValue) Stream<T?>
save(T value) Future<void>
toString() String
A string representation of this object.
inherited
update(T updater(T? data)) Future<T>
Convenience method which allows simple updating of data. The updater gets the current value as parameter and is expected to return a copy with the new values which will be persisted afterwards.

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

getForTypeSync<T extends HasToJson>(FromJson<T> fromJson, {T defaultCreator()?, required String? name, String? customName, StoreBackend? storeBackend}) SimpleJsonPersistence<T>
Creates a new persistence store for the given type. The json file location can be customized by passing in a custom storeBackend and create it using StoreBackend.create.
getForTypeWithDefault<T extends HasToJson>(FromJson<T> fromJson, {required T defaultCreator(), required String? name, String? customName, StoreBackend? storeBackend}) SimpleJsonPersistenceWithDefault<T>