PersistedSignal#
class |
Package: package:signals_core
Class: PersistedSignal#
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:
- PersistedBoolSignal / PersistedNullableBoolSignal
- PersistedIntSignal / PersistedNullableIntSignal
- PersistedDoubleSignal / PersistedNullableDoubleSignal
- PersistedNumSignal / PersistedNullableNumSignal
- PersistedStringSignal / PersistedNullableStringSignal
- PersistedEnumSignal / PersistedNullableEnumSignal
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());
}
Members of PersistedSignal#
| Member | Type | Signature | Description |
|---|---|---|---|
| PersistedSignal | constructor |
dart PersistedSignal(super.internalValue, {required this.key, required this.store, PersistedSignalOptions
|
Creates a new PersistedSignal. |
| key | field |
dart String key |
|
| store | field |
dart SignalsKeyValueStore store |
References#
The PersistedSignal type is referenced and used in the following pages:
- Persisted Signals (guides)
- PersistedSignal (signals_flutter/utilities)
- signals_flutter
- PersistedSignal (signals_core/utilities)
- signals_core
- PersistedSignal (signals/utilities)
- signals