Store class abstract

An abstract base class for creating stores that hold application state with persistence capabilities.

Extend this class to define a new store. Each store is a singleton that manages a specific part of the application's state.

How to Use

  1. Create a Store Class: Extend Store and implement the id getter and the persistents() method.

  2. Define State: Use Ref<T> from the kaeru library to define state variables.

  3. Specify Persistent State: Return the list of Refs that should be persisted in the persistents() method.

Full Example

// stores/counter.dart
import 'package:kaeru/kaeru.dart';
import 'package:kaeru_store/kaeru_store.dart';

class CounterStore extends Store {
  // A unique ID for the store, used as the filename.
  @override
  String get id => 'counter';

  // Define a reactive state variable.
  late final count = Ref<int>(0);

  // Specify that `count` should be persisted.
  @override
  List<Ref> persistents() => [count];
}

// Create a single instance of the store.
final counterStore = CounterStore();

Preloading Data

If you need to ensure data is restored before the UI is built, use the static Store.preload() method.

// main.dart
void main() async {
  // Ensure Flutter is initialized.
  WidgetsFlutterBinding.ensureInitialized();

  // Preload the data for the counterStore.
  await Store.preload([counterStore]);

  runApp(MyApp());
}

Constructors

Store()

Properties

filename → String
The filename where the store's data is saved. Defaults to [id].data.
no setter
hashCode → int
The hash code for this object.
no setterinherited
id → String
A unique ID for the store. This value is used to create the storage filename, so it must be unique among all stores.
no setter
privateKeyScope → String
The encryption password specific to this store. By default, it uses the global Store.privateKey. Override this getter to provide a different password for a specific store.
no setter
restoring ↔ Future<void>?
A Future that completes when the initial data restoration is finished.
getter/setter pair
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
storageScope → StorageBase
The storage instance specific to this store. By default, it uses the global Store.storage. Override this getter to provide a different storage mechanism.
no setter

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
persistents() → List<Ref>
Returns a list of Ref variables that should be persisted.
toString() → String
A string representation of this object.
inherited

Operators

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

Static Properties

dir ↔ String
The directory where store files will be saved.
getter/setter pair
privateKey ↔ String
The global password used to encrypt data for all stores. If left empty, encryption is disabled.
getter/setter pair
salt ↔ String
The "salt" string used during key derivation from the password. Change this value to enhance security.
getter/setter pair
storage ↔ StorageBase
The global storage instance used by all stores. Defaults to StorageDefault, which uses the file system on mobile/desktop and localStorage on the web.
getter/setter pair

Static Methods

preload(List<Store> stores) → Future<void>
Preloads and restores data for a list of stores.