NativeStorage constructor

NativeStorage({
  1. String? namespace,
  2. String? scope,
})

Provides app-local storage of key-value pairs.

The values written to this storage are persisted across app reloads for the lifetime of the app on the end user's device. Unlike NativeSecureStorage, which may persist values after an app is uninstalled, values written to this storage are guaranteed to be removed when the app is no longer present on the device.

Namespace

The main identifier all values are stored under.

To avoid conflicts with other storage instances, this value should be unique to the app or package. It is recommended to always use your application or bundle identifier, which is the default if not passed.

If provided, it must match the regular expression ^\w+(\.\w+)*$, which is that of a typical bundle identifier.

Scope

An optional tag for separating values for different parts of your app.

This can be passed to NativeStorage.new or dynamically created via scoped which will create a subscope if this is not null.

Scopes are separated by a / character and, unlike namespace, are used to create a logical, but not physical, separation of values. For example, if scope is settings, calling storage.write('theme', 'dark') will store the value under the storage key settings/theme.

Implementation

factory NativeStorage({
  String? namespace,
  String? scope,
}) {
  validateNamespace(namespace);
  final instance = NativeLocalStoragePlatform(
    namespace: namespace,
    scope: scope,
  );
  return instances[(instance.namespace, scope)] ??= instance;
}