在Flutter中使用嵌套导航器时,可能会出现此错误。这是因为您的代码中使用了多个嵌套导航器,并且它们在导航时出现了冲突。
要解决此问题,可以尝试使用一个全局的嵌套导航器,而不是多个嵌套导航器。或者,在每个嵌套导航器中使用“routeInformationParser”和“routerDelegate”参数,以确保嵌套导航器之间的通信正确。
以下是示例代码:
class AppRouterDelegate extends RouterDelegate
AppRouterDelegate({ required this.navigatorKey, required AppRouteInformationParser routeInformationParser, }) : _routeInformationParser = routeInformationParser { _routeInformationParser.addListener(() { notifyListeners(); }); }
@override MyAppPath get currentConfiguration {
// return MyAppPath.home();
}
@override Widget build(BuildContext context) { return Navigator( key: navigatorKey, pages: [ MaterialPage( child: Center( child: ElevatedButton( onPressed: () { context.router.push(const SecondRoute()); }, child: const Text('Go to second route'), ), ), ), ); } }
class AppRouteInformationParser extends RouteInformationParser
// Handle '/'
if (uri.pathSegments.length == 0) {
return MyAppPath.home();
}
// Handle '/second'
if (uri.pathSegments.length == 1 &&
uri.pathSegments.first == 'second') {
return MyAppPath.second();
}
// Handle unknown routes
return