persist_notifier 1.0.0 persist_notifier: ^1.0.0 copied to clipboard
An extension of ValueNotifier that will persist the value in storage. It was designed for defining settings and providing a 1-line solution with a default value and notification system.
Persist Notifier plugin #
Wraps platform-specific persistent storage for simple data (NSUserDefaults on iOS and macOS, SharedPreferences on Android, etc.) using existing ValueNotifier methods and newly defined methods for additional functionality. Data may be persisted to disk asynchronously or synchronously.
Supported data types are int
, double
, bool
, String
and List<String>
.
For information on storage of the values, see the documentation on SharedPreferences.
Usage #
To use this plugin, add persist_notifier
as a dependency in your pubspec.yaml file.
Examples #
Here are small examples that show you how to use the API.
Definition
// Define as static or member variable
PersistNotifier pn = PersistNotifier("com.example.test", 0);
void example() async {
// Define async
var pn = PersistNotifier("com.example.test", 0);
// Define sync
var pn = await PersistNotifier.create("com.example.test", 0);
}
Read data
// Read in the default or stored value
PersistNotifier pn = await PersistNotifier.create("com.example.test", 0);
final int counter = pn.value;
// Force a sync of the stored value if the underlying value as changed
pn.resync();
Write data
// Read in the default or stored value
PersistNotifier pn = await PersistNotifier.create("com.example.test", 0);
pn.value = 7; // Async storage update
bool success = await pn.set(7); // Sync storage update
Remove an entry
// Reset the value back to the default
PersistNotifier pn = await PersistNotifier.create("com.example.test", 0);
pn.value = 7;
pn.reset();
// pn.value == 0
Storage location by platform #
Platform | Location |
---|---|
Android | SharedPreferences |
iOS | NSUserDefaults |
Linux | In the XDG_DATA_HOME directory |
macOS | NSUserDefaults |
Web | LocalStorage |
Windows | In the roaming AppData directory |