Skip to content

Commit

Permalink
Add more to error message of RestorationScope.of (flutter#126444)
Browse files Browse the repository at this point in the history
  • Loading branch information
Piinks authored and Casey Hillers committed May 24, 2023
1 parent a93c7d7 commit ae89a79
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
29 changes: 20 additions & 9 deletions packages/flutter/lib/src/widgets/restoration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,26 @@ class RestorationScope extends StatefulWidget {
final RestorationBucket? bucket = maybeOf(context);
assert(() {
if (bucket == null) {
throw FlutterError(
'RestorationScope.of() was called with a context that does not contain a '
'RestorationScope widget.\n'
'No RestorationScope widget ancestor could be found starting from the '
'context that was passed to RestorationScope.of(). This can happen '
'because you are using a widget that looks for a RestorationScope '
'ancestor, but no such ancestor exists.\n'
'The context used was:\n'
' $context',
throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary(
'RestorationScope.of() was called with a context that does not '
'contain a RestorationScope widget. '
),
ErrorDescription(
'No RestorationScope widget ancestor could be found starting from '
'the context that was passed to RestorationScope.of(). This can '
'happen because you are using a widget that looks for a '
'RestorationScope ancestor, but no such ancestor exists.\n'
'The context used was:\n'
' $context'
),
ErrorHint(
'State restoration must be enabled for a RestorationScope to exist. '
'This can be done by passing a restorationScopeId to MaterialApp, '
'CupertinoApp, or WidgetsApp at the root of the widget tree or by '
'wrapping the widget tree in a RootRestorationScope.'
),
],
);
}
return true;
Expand Down
46 changes: 46 additions & 0 deletions packages/flutter/test/widgets/restoration_scope_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,52 @@ void main() {
});

group('RestorationScope', () {
testWidgets('asserts when none is found', (WidgetTester tester) async {
late BuildContext capturedContext;
await tester.pumpWidget(WidgetsApp(
color: const Color(0xD0FF0000),
builder: (_, __) {
return RestorationScope(
restorationId: 'test',
child: Builder(
builder: (BuildContext context) {
capturedContext = context;
return Container();
}
)
);
},
));
expect(
() {
RestorationScope.of(capturedContext);
},
throwsA(isA<FlutterError>().having(
(FlutterError error) => error.message,
'message',
contains('State restoration must be enabled for a RestorationScope'),
)),
);

await tester.pumpWidget(WidgetsApp(
restorationScopeId: 'test scope',
color: const Color(0xD0FF0000),
builder: (_, __) {
return RestorationScope(
restorationId: 'test',
child: Builder(
builder: (BuildContext context) {
capturedContext = context;
return Container();
}
)
);
},
));
final UnmanagedRestorationScope scope = tester.widget(find.byType(UnmanagedRestorationScope).last);
expect(RestorationScope.of(capturedContext), scope.bucket);
});

testWidgets('makes bucket available to descendants', (WidgetTester tester) async {
const String id = 'hello world 1234';
final MockRestorationManager manager = MockRestorationManager();
Expand Down

0 comments on commit ae89a79

Please sign in to comment.