RestorableValue<T> class Null safety
A RestorableProperty that makes the wrapped value accessible to the owning State object via the value getter and setter.
Whenever a new value is set, didUpdateValue is called. Subclasses should call notifyListeners from this method if the new value changes what toPrimitives returns.
Using a RestorableValue
A StatefulWidget that has a restorable int property.
To create a local project with this code sample, run:
flutter create --sample=widgets.RestorableValue.1 mysample
flutter create --sample=widgets.RestorableValue.1 mysample
A StatefulWidget that has a restorable int property.
// The current value of the answer is stored in a [RestorableProperty].
// During state restoration it is automatically restored to its old value.
// If no restoration data is available to restore the answer from, it is
// initialized to the specified default value, in this case 42.
RestorableInt _answer = RestorableInt(42);
@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
// All restorable properties must be registered with the mixin. After
// registration, the answer either has its old value restored or is
// initialized to its default value.
registerForRestoration(_answer, 'answer');
}
void _incrementAnswer() {
setState(() {
// The current value of the property can be accessed and modified via
// the value getter and setter.
_answer.value += 1;
});
}
@override
void dispose() {
// Properties must be disposed when no longer used.
_answer.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return OutlinedButton(
child: Text('${_answer.value}'),
onPressed: _incrementAnswer,
);
}
Creating a subclass
This example shows how to create a new
RestorableValue
subclass,
in this case for the Duration class.
class RestorableDuration extends RestorableValue<Duration> {
@override
Duration createDefaultValue() => const Duration();
@override
void didUpdateValue(Duration? oldValue) {
if (oldValue == null || oldValue.inMicroseconds != value.inMicroseconds)
notifyListeners();
}
@override
Duration fromPrimitives(Object? data) {
if (data != null) {
return Duration(microseconds: data as int);
}
return const Duration();
}
@override
Object toPrimitives() {
return value.inMicroseconds;
}
}
See also:
- RestorableProperty, which is the super class of this class.
- RestorationMixin, to which a RestorableValue needs to be registered in order to work.
- RestorationManager, which provides an overview of how state restoration works in Flutter.
- Inheritance
- Object
- ChangeNotifier
- RestorableProperty<
T> - RestorableValue
- Implementers
Constructors
Properties
- enabled → bool
-
Whether the object currently returned by toPrimitives should be included
in the restoration state. [...]
read-only, inherited
- hashCode → int
-
The hash code for this object. [...]
read-only, inherited
- hasListeners → bool
-
Whether any listeners are currently registered. [...]
@protected, read-only, inherited
- isRegistered → bool
-
Whether this property is currently registered with a RestorationMixin.
@protected, read-only, inherited
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
-
state
→ State<
StatefulWidget> -
The State object that this property is registered with. [...]
@protected, read-only, inherited
- value ↔ T
-
The current value stored in this property. [...]
read / write
Methods
-
addListener(
VoidCallback listener) → void -
Register a closure to be called when the object changes. [...]
inherited
-
createDefaultValue(
) → T -
Called by the RestorationMixin if no restoration data is available to
restore the value of the property from to obtain the default value for the
property. [...]
inherited
-
didUpdateValue(
T? oldValue) → void - Called whenever a new value is assigned to value. [...]
-
dispose(
) → void -
Discards any resources used by the object. After this is called, the
object is not in a usable state and should be discarded (calls to
addListener and removeListener will throw after the object is
disposed). [...]
inherited
-
fromPrimitives(
Object? data) → T -
Called by the RestorationMixin to convert the
data
previously retrieved from toPrimitives back into an object of typeT
that this property should wrap. [...]inherited -
initWithValue(
T value) → void -
Called by the RestorationMixin with the
value
returned by either createDefaultValue or fromPrimitives to set the value that this property currently wraps. [...]@mustCallSuper, override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
-
notifyListeners(
) → void -
Call all the registered listeners. [...]
@protected, @visibleForTesting, inherited
-
removeListener(
VoidCallback listener) → void -
Remove a previously registered closure from the list of closures that are
notified when the object changes. [...]
inherited
-
toPrimitives(
) → Object? -
Called by the RestorationMixin to retrieve the information that this
property wants to store in the restoration data. [...]
inherited
-
toString(
) → String -
A string representation of this object. [...]
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator. [...]
inherited