useRestoration function
RestorationMixin<StatefulWidget>
useRestoration({
- String? restorationId,
- required void restoreState(
- RestorationBucket? oldBucket,
- bool initialRestore
- required void didToggleBucket(
- RestorationBucket? oldBucket
- void registerForRestoration(
- RestorableProperty<
Object?> property, - String restorationId
- RestorableProperty<
- void unregisterFromRestoration(
- RestorableProperty<
Object?> property
- RestorableProperty<
- VoidCallback? didUpdateRestorationId,
A hook that provides restoration capabilities for widgets.
This hook allows you to manage the restoration of state across app restarts and other lifecycle events. It integrates with the RestorationMixin to enable automatic restoration of properties that need to be preserved.
Example:
final restoration = useRestoration(
restorationId: 'my_widget',
restoreState: (oldBucket, initialRestore) {
// Restore your state here
},
didToggleBucket: (oldBucket) {
// Handle bucket toggling here
},
);
Implementation
RestorationMixin useRestoration({
/// An optional identifier for the restoration bucket. This ID is used to
/// associate the restoration state with the corresponding widget.
String? restorationId,
required void Function(RestorationBucket? oldBucket, bool initialRestore)
restoreState,
required void Function(RestorationBucket? oldBucket) didToggleBucket,
void Function(RestorableProperty<Object?> property, String restorationId)?
registerForRestoration,
void Function(RestorableProperty<Object?> property)?
unregisterFromRestoration,
VoidCallback? didUpdateRestorationId,
}) {
final context = useContext();
final restoration = _Restoration(context,
restorationId: restorationId,
restoreState: restoreState,
didToggleBucket: didToggleBucket,
registerForRestoration: registerForRestoration,
unregisterFromRestoration: unregisterFromRestoration,
didUpdateRestorationId: didUpdateRestorationId);
onUpdated(restoration.didUpdateWidget);
onDependenciesChanged(restoration.didChangeDependencies);
onBeforeUnmount(restoration.dispose);
return restoration;
}