Flutter / Inclusive interfaces and target evidence / Lesson 21
State restoration scope and lifetime
Restore small UI state with a Flutter restoration scope while separating it from durable application data.
On this page
Lesson goals
- Provide a restoration scope and register a restorable value.
- Explain UI restoration and durable storage boundaries.
- Test process restoration on the target.
Choose state to restore
A platform may destroy a backgrounded application process and later recreate its interface. Flutter restoration can help recover small transient UI state when the host supports it. Decide which state should restore and which data must instead be stored durably. Restoration is not a confirmed server write, a backup system or authorization to redisplay private data after a session changes.
Provide a restoration scope
The application fragment gives MaterialApp a restoration scope ID and displays the counter defined below. The host must include that class and a normal Flutter entry point. A scope organizes restoration data for descendants; it does not automatically make every arbitrary value or navigation operation restorable. Platform setup and behavior must be checked for the intended target.
import "package:flutter/material.dart";
class WorkshopApp extends StatelessWidget {
const WorkshopApp({super.key});
@override
Widget build(BuildContext context) => const MaterialApp(
restorationScopeId: "workshop",
home: Scaffold(body: Center(child: RestorableCounter())),
);
}
The host creates a restoration scope named workshop.
The application enables a root restoration scope for its descendant counter.
Register and dispose a value
The counter owns a RestorableInt, registers it under a stable ID in restoreState and disposes it with the State object. The button changes the restorable value through setState. IDs must be unique in the relevant scope. This is illustrative code and its platform restoration behavior is not claimed as executed; a complete host must import both fragments.
import "package:flutter/material.dart";
class RestorableCounter extends StatefulWidget {
const RestorableCounter({super.key});
@override
State<RestorableCounter> createState() => _RestorableCounterState();
}
class _RestorableCounterState extends State<RestorableCounter> with RestorationMixin {
final count = RestorableInt(0);
@override
String get restorationId => "counter";
@override
void restoreState(RestorationBucket? oldBucket, bool initialRestore) { registerForRestoration(count, "count"); }
@override
void dispose() { count.dispose(); super.dispose(); }
@override
Widget build(BuildContext context) => TextButton(onPressed: () => setState(() => count.value += 1), child: Text("Count: ${count.value}"));
}
The counter begins at Count: 0 and increments when activated.
The mixin registers count in the surrounding scope and the owner disposes the restorable property.
Verify on the platform
Use Flutter restoration tests where appropriate and exercise process restoration on the intended device or simulator using the official platform instructions. Change the synthetic count, trigger the supported restoration scenario and confirm the expected value. Test a fresh launch and changed session separately. Record the platform and test setup and restore any temporary device settings afterward.
Common errors and fixes
The counter returns to its initial value.
- Why it happens
- The scope, restoration ID or property registration is missing or mismatched.
- How to fix it
- Provide the scope and register the value with stable IDs.
- Prevent it next time
- Test a full restoration scenario.
Restored content belongs to an old session.
- Why it happens
- Private state was shown without checking the current session.
- How to fix it
- Clear or revalidate it at the session boundary.
- Prevent it next time
- Test sign-out and a different synthetic user.
The route is not restored with the counter.
- Why it happens
- Restorable UI data was mistaken for navigation restoration.
- How to fix it
- Use a supported restorable navigation design.
- Prevent it next time
- Test the destination and data independently.
2 practice exercises
1. Does restoring a counter prove it was saved to a server?
Show worked solution
No. Restoration recovers UI state through the platform restoration mechanism. A durable server save requires its own operation and confirmation.
2. Why dispose the RestorableInt?
Show worked solution
The State owns the restorable property and should release it when the owner is disposed. Register and dispose it according to the Flutter lifecycle.
Key takeaways
- Restore only appropriate small UI state.
- Register stable IDs in a restoration scope.
- Test process, session and navigation boundaries separately.