Persist Notifier

pub package

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 has changed.
// This should only happen if it's changed in a separate variable with
// the same key.
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

Extend the provided manager for settings

class SettingsManager extends PersistNotifierManager {
    // Make a singleton for easier access
    ...
    PersistNotifier pn = PersistNotifier("com.example.test", 0);
    PersistNotifier pnBoo = PersistNotifier("com.example.boo", 7);

    SettingsManager() {
        add(pn);
        add(pnBoo, group: "boo");
    }
}

void example() {
    SettingsManager manager = SettingsManager();
    manager.reset(group: "boo"); // Resets only the "boo" group
    manager.reset(); // Resets all
    List<PersistNotifier> list;
    list = manager.getAll(group: "boo"); // Gets only the "boo" group
    list = manager.getAll(); // Gets all groups combined
}

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

Libraries

persist_notifier
A ValueNotifier designed for defining settings and providing a solution with a default value and notification system. Includes a manager for the general management of settings.