Add more to error message of RestorationScope.of (#126444)
Fixes https://github.com/flutter/flutter/issues/126443
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user