diff --git a/packages/flutter/lib/src/widgets/restoration.dart b/packages/flutter/lib/src/widgets/restoration.dart index 1afb5b3984..101e6422da 100644 --- a/packages/flutter/lib/src/widgets/restoration.dart +++ b/packages/flutter/lib/src/widgets/restoration.dart @@ -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([ + 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; diff --git a/packages/flutter/test/widgets/restoration_scope_test.dart b/packages/flutter/test/widgets/restoration_scope_test.dart index e9550ef9bc..74ab521fc5 100644 --- a/packages/flutter/test/widgets/restoration_scope_test.dart +++ b/packages/flutter/test/widgets/restoration_scope_test.dart @@ -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().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();