persist<I> method

void persist<I>(
  1. String key, {
  2. FastRxPersistenceInterface? store,
  3. PersistenceConverter<T, I>? converter,
})

Persist the value though the store

  • I - The internal type of the value stored in the store
  • key - The key used to store the value
  • store - An optional override of the default FastRxPersistenceInterface given with FastRxPersistence.init
  • converter - A converter to transform the value to and from the store

On initialization, the store is asked for an existing value for the key. If one exists, the current value is replaced and listeners are notified. If no value exists, then the given value is left as the default.

The value is saved through the store when the notification stream emits an update

Implementation

void persist<I>(
  String key, {
  FastRxPersistenceInterface? store,
  PersistenceConverter<T, I>? converter,
}) {
  final interface = store ?? FastRxPersistence.store;

  final storeValue = interface.get(key);
  if (storeValue != null) {
    final transformedValue =
        converter?.fromStore(storeValue as I) ?? storeValue as T;

    if (this is RxObject) {
      // ignore: invalid_use_of_protected_member
      (this as RxObject).internalSetValue(transformedValue);
      notify();
    } else {
      value = transformedValue;
    }
  }

  stream.listen((value) {
    final storeValue = converter?.toStore(value) ?? value as I;
    interface.set(key, storeValue);
  });
}