在Flutter应用程序中尝试从一个视图导航到另一个视图时,出现异常。
I/flutter ( 2199): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter ( 2199): The following assertion was thrown while handling a gesture:
I/flutter ( 2199): Could not find a generator for route "home-page" in the _MaterialAppState.
答案 0 :(得分:10)
使用
Navigator.push(context, new MaterialPageRoute(
builder: (context) =>
new MyHomePage())
);
代替
Navigator.of(context).pushNamed('/home-page');
//or
Navigator.pushedName(context, '/home-page');
答案 1 :(得分:0)
错误说Could not find a generator for route "home-page" in the _MaterialAppState.
。当您使用 NamedRoute (从错误消息中推断)时,我认为问题出在 route 设置上。请参阅示例以设置路线,
MaterialApp(
title: 'Named Routes Demo',
initialRoute: '/',
routes: { //route setting
'/': (context) => FirstScreen(),
'/home-page': (context) => HomePage(), //you should have something like this.
},
)
答案 2 :(得分:0)
尝试
Navigator.push(context, new MaterialPageRoute(builder: (context) =>new PageName())
答案 3 :(得分:0)
此消息告诉您,在路由列表中未列出您搜索的路由。因此,请检查您的MaterialApp->路线中是否具有指定的路线。
答案 4 :(得分:0)
您需要在特定的dart文件中定义要从其跳转到下一个屏幕的路径。以您的情况为例,有三个屏幕: 1. mainScreen.dart 2.登录Screen.dart 3.TabScreen.dart
现在,您可能已经在mainscreen.dart中为Loginscreen和TabScreen定义了路由,例如:
routes : <String, WidgetBuilder>{
'/login' : (BuildContext context)=> LoginScreen()
'/tab' : (BuildContext context)=> TabScreen()
}
,您尝试从LoginScreen跳到TabScreen,但尚未在LoginScreen.dart中定义TabScreen的路由
请确保您已在LoginScreen中为TabScreen定义了路由:
routes : <String, WidgetBuilder>{
'/tab' : (BuildContext context)=> TabScreen()
}