Flutter RestorationBucket value missing after process death
0 reputation · 17 Jun 2022, 06:15 UTC
0 reputation · 17 Jun 2022, 06:15 UTC
Determine whether a value registered with RestorationBucket in a Flutter WidgetsApp restoration scope is expected to survive process death, and how to detect when restored data is absent or corrupted.
Flutter state restoration is scoped to UI state registered with restoration IDs and is best-effort; platforms may discard the restoration bucket at any time. The framework provides no built-in checksum, schema versioning, or integrity verification, so restored values must be validated by the developer. Restoration IDs must be unique within the restoration scope to avoid overwrites or failed restoration.
It is unclear whether a missing restored value should be treated as normal eviction, a registration error, or data corruption, and what validation pattern fits the restoration lifecycle without treating restoration state as durable user-data backup.
What conditions cause RestorationBucket values to be discarded after process death? How can restored values be validated for schema and integrity during restoreState? Should absence of a restored value trigger a default state or a full reinitialization?
37275 reputation · 17 Jun 2022, 08:49 UTC
No. A RestorationBucket value is not guaranteed to survive process death. Restoration data is best-effort UI state owned by the platform, so a missing value after restart is expected behavior—not proof of a registration error or corruption.
finish() or back press on root)restoreState/fromPrimitives is never called—the property keeps its initial value.fromPrimitives receiving null is the framework's explicit "not restored" signal.int, double, String, bool, List, Map) or they silently fail to restore.restorationId values within a scope cause claim failures or silent overwrites.restoreStateclass _MyRestorableInt extends RestorableProperty<int> {
@override
int createDefaultValue() => 0;
@override
void fromPrimitives(Object? data) {
if (data == null) {
value = createDefaultValue(); // not restored
return;
}
try {
final map = data as Map<String, Object?>;
final version = map['version'] as int?;
if (version != 1) throw FormatException('version mismatch');
final raw = map['data'];
if (raw is! int) throw FormatException('type mismatch');
value = raw;
} catch (_) {
value = createDefaultValue(); // treat corruption as not restored
}
}
@override
Object? toPrimitives() => {
'version': 1,
'data': value,
};
}
WidgetsApp or MaterialApp has a non-null restorationScopeIdRestorableProperty and child bucket has a unique restorationId within its parent scopetoPrimitives outputIf you are seeing consistent loss even on normal background/restore cycles (not force-quit/swipe-away), share the restorationScopeId value and a minimal reproduction of the bucket registration—ID collisions or missing scope IDs are the most common cause of silent restoration failure.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.