LogoSignals.dart
Copy Markdown
rodydavis/signals.dart 999999

PersistedSignal

A <code>Signal</code> whose value is persistently stored in a key-value database.

A Signal whose value is persistently stored in a key-value database.

PersistedSignal allows application state (such as user preferences, theme options, authentication tokens, and drafts) to automatically survive application restarts without writing tedious boilerplate for manual loading and saving.

Concrete Subclasses#

For common primitive types, use the provided concrete classes:

Simple Usage Example#

// 1. Create or obtain a key-value store adapter (like standard in-memory)
final localStore = SignalsInMemoryKeyValueStore();

// 2. Create the persisted signal with a unique key
final darkModeSignal = PersistedBoolSignal(
  false, // Fallback initial value
  key: 'settings.dark_mode',
  store: localStore,
);

// 3. The value is automatically loaded asynchronously on instantiation.
// Mutating the value synchronously schedules an async save under the hood:
darkModeSignal.value = true; // Automatically persisted to store

Custom Serialization / Complex Objects#

To persist complex objects (e.g. custom classes), subclass PersistedSignal and override the decode and encode methods, or mixin PersistedSignalMixin on a custom Signal class.

class User {
  final String name;
  final int age;
  User(this.name, this.age);

  Map<String, dynamic> toJson() => {'name': name, 'age': age};
  factory User.fromJson(Map<String, dynamic> json) => User(json['name'], json['age']);
}

class PersistedUserSignal extends PersistedSignal<User> {
  PersistedUserSignal(
    super.internalValue, {
    required super.key,
    required super.store,
  });

  @override
  User decode(String value) => User.fromJson(jsonDecode(value));

  @override
  String encode(User value) => jsonEncode(value.toJson());
}

Constructors#

View Constructors
PersistedSignal(super.internalValue, {required this.key, required this.store, PersistedSignalOptions<T>? options, @Deprecated('Use options: PersistedSignalOptions(autoDispose: ...) instead') bool? autoDispose, @Deprecated('Use options: PersistedSignalOptions(name: ...) instead') String? debugLabel, bool autoInit = true})

Creates a new PersistedSignal.

Properties#

View Properties
String key
SignalsKeyValueStore store

SignalsKeyValueStore#

An abstract class defining the persistence adapter contract for PersistedSignal.

Implement this interface to bind PersistedSignal to your storage engine of choice, such as local files, SQLite, SharedPreferences, Hive, or indexedDB.

Example: Custom Shared Preferences Store (Flutter)#

import 'package:shared_preferences/shared_preferences.dart';
import 'package:signals/signals.dart';

class SharedPreferencesStore implements SignalsKeyValueStore {
  final SharedPreferences prefs;
  SharedPreferencesStore(this.prefs);

  @override
  Future<String?> getItem(String key) async {
    return prefs.getString(key);
  }

  @override
  Future<void> setItem(String key, String value) async {
    await prefs.setString(key, value);
  }

  @override
  Future<void> removeItem(String key) async {
    await prefs.remove(key);
  }
}

Properties#

View Properties
static SignalsKeyValueStore defaultStore

The default store to be used if no store is provided.

Methods#

View Methods
Future<void> setItem(String key, String value)

Sets an item in the store.

Future<String?> getItem(String key)

Gets an item from the store.

Future<void> removeItem(String key)

Removes an item from the store.


PersistedSignalMixin#

A mixin that adds local persistence capabilities to a standard Signal.

By mixing in PersistedSignalMixin on a Signal subclass, the signal will automatically retrieve its stored state on boot and save its state whenever .value is mutated.

Classes mixing in PersistedSignalMixin must implement:

  • key: A unique identifier string for the key-value database.
  • store: An implementation of SignalsKeyValueStore.

Serialization Customization#

By default, the mixin uses standard JSON parsing (jsonDecode / jsonEncode). If your data type T is not natively supported by JSON, override:

  • decode to convert the raw string value back into type T.
  • encode to serialize type T into a string.

Properties#

View Properties
bool loaded

Whether the signal has been loaded from the store.

Methods#

View Methods
String key

The key to use for storing the value.

SignalsKeyValueStore store

The store to use for storing the value.

Future<void> init()

Initializes the signal by loading the value from the store.

T value
value(T value)
Future<T> load()

Loads the value from the store.

Future<void> save(T value)

Saves the value to the store.

T decode(String value)

Decodes the value from a string.

String encode(T value)

Encodes the value to a string.


SignalsInMemoryKeyValueStore#

An in-memory, volatile implementation of SignalsKeyValueStore.

This serves as a fallback engine and does not persist across restarts/reload.

Properties#

View Properties
store

The in-memory store.

Methods#

View Methods
Future<String?> getItem(String key)
Future<void> removeItem(String key)
Future<void> setItem(String key, String value)

PersistedNullableStringSignal#

A PersistedSignal that stores a nullable string value.

[!warning] An empty value is considered null

Constructors#

View Constructors
PersistedNullableStringSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new NullableStringSignal.

Methods#

View Methods
String? decode(String value)
String encode(String? value)

PersistedNullableIntSignal#

A PersistedSignal that stores a nullable integer value.

Constructors#

View Constructors
PersistedNullableIntSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new NullableIntSignal.

Methods#

View Methods
int? decode(String value)
String encode(int? value)

PersistedNullableNumSignal#

A PersistedSignal that stores a nullable numeric value.

Constructors#

View Constructors
PersistedNullableNumSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new NullableNumSignal.

Methods#

View Methods
num? decode(String value)
String encode(num? value)

PersistedNullableBoolSignal#

A PersistedSignal that stores a nullable string value.

Constructors#

View Constructors
PersistedNullableBoolSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new NullableBoolSignal.

Methods#

View Methods
bool? decode(String value)
String encode(bool? value)

PersistedNullableDoubleSignal#

A PersistedSignal that stores a nullable double value.

Constructors#

View Constructors
PersistedNullableDoubleSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new NullableDoubleSignal.

Methods#

View Methods
double? decode(String value)
String encode(double? value)

PersistedNullableEnumSignal#

A PersistedSignal that stores a nullable enum value.

Constructors#

View Constructors
PersistedNullableEnumSignal(super.val, String key, this.values, {SignalsKeyValueStore? store})

Creates a new NullableEnumSignal.

Properties#

View Properties
List<T> values

Methods#

View Methods
T? decode(String value)
String encode(T? value)

PersistedIntSignal#

A PersistedSignal that stores an integer value.

Constructors#

View Constructors
PersistedIntSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new IntSignal.

Methods#

View Methods
int decode(String value)
String encode(int value)

PersistedBoolSignal#

A PersistedSignal that stores a boolean value.

Constructors#

View Constructors
PersistedBoolSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new BoolSignal.

Methods#

View Methods
bool decode(String value)
String encode(bool value)

PersistedDoubleSignal#

A PersistedSignal that stores an double value.

Constructors#

View Constructors
PersistedDoubleSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new DoubleSignal.

Methods#

View Methods
double decode(String value)
String encode(double value)

PersistedNumSignal#

A PersistedSignal that stores a numeric value.

Constructors#

View Constructors
PersistedNumSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new NumSignal.

Methods#

View Methods
num decode(String value)
String encode(num value)

PersistedStringSignal#

A PersistedSignal that stores a string value.

Constructors#

View Constructors
PersistedStringSignal(super.val, String key, {SignalsKeyValueStore? store})

Creates a new StringSignal.

Methods#

View Methods
String decode(String value)
String encode(String value)

PersistedEnumSignal#

A PersistedSignal that stores an enum value.

Constructors#

View Constructors
PersistedEnumSignal(super.val, String key, this.values, {SignalsKeyValueStore? store})

Creates a new EnumSignal.

Properties#

View Properties
List<T> values

Methods#

View Methods
T decode(String value)
String encode(T value)

PersistedSignalOptions#

Configuration options for a PersistedSignal.

Constructors#

View Constructors
PersistedSignalOptions({super.name, super.autoDispose, super.watched, super.unwatched})

Creates a new PersistedSignalOptions instance.

Methods#

View Methods
PersistedSignalOptions<T> copyWith({String? name, bool? autoDispose, void Function()? watched, void Function()? unwatched})
bool ==(Object other)
int hashCode