Navigator assert on non restorable routes returned by onGenerateInitialRoutes (#73082)
This commit is contained in:
@@ -3388,6 +3388,13 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
|
||||
}
|
||||
}
|
||||
|
||||
assert(
|
||||
_history.isNotEmpty,
|
||||
'All routes returned by onGenerateInitialRoutes are not restorable. '
|
||||
'Please make sure that all routes returned by onGenerateInitialRoutes '
|
||||
'have their RouteSettings defined with names that are defined in the '
|
||||
'app\'s routes table.',
|
||||
);
|
||||
assert(!_debugLocked);
|
||||
assert(() { _debugLocked = true; return true; }());
|
||||
_flushHistoryUpdates();
|
||||
|
||||
@@ -981,6 +981,44 @@ void main() {
|
||||
await tester.pumpAndSettle();
|
||||
expect(findRoute('p1', count: 0), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Helpful assert thrown all routes in onGenerateInitialRoutes are not restorable', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
restorationScopeId: 'material_app',
|
||||
initialRoute: '/',
|
||||
routes: <String, WidgetBuilder>{
|
||||
'/': (BuildContext context) => Container(),
|
||||
},
|
||||
onGenerateInitialRoutes: (String initialRoute) {
|
||||
return <MaterialPageRoute<void>>[
|
||||
MaterialPageRoute<void>(
|
||||
builder: (BuildContext context) => Container(),
|
||||
),
|
||||
];
|
||||
},
|
||||
),
|
||||
);
|
||||
await tester.restartAndRestore();
|
||||
final dynamic exception = tester.takeException();
|
||||
expect(exception, isAssertionError);
|
||||
expect(
|
||||
(exception as AssertionError).message,
|
||||
contains('All routes returned by onGenerateInitialRoutes are not restorable.'),
|
||||
);
|
||||
|
||||
// The previous assert leaves the widget tree in a broken state, so the
|
||||
// following code catches any remaining exceptions from attempting to build
|
||||
// new widget tree.
|
||||
final FlutterExceptionHandler? oldHandler = FlutterError.onError;
|
||||
dynamic remainingException;
|
||||
FlutterError.onError = (FlutterErrorDetails details) {
|
||||
remainingException ??= details.exception;
|
||||
};
|
||||
await tester.pumpWidget(Container(key: UniqueKey()));
|
||||
FlutterError.onError = oldHandler;
|
||||
expect(remainingException, isAssertionError);
|
||||
});
|
||||
}
|
||||
|
||||
Route<void> _routeBuilder(BuildContext context, Object? arguments) {
|
||||
|
||||
Reference in New Issue
Block a user