Could not find a generator for route “home-page” in the _MaterialAppState

馋奶兔 提交于 2020-01-03 00:57:12

问题


I am getting exception when I trying to navigate from one view to another in flutter app.

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.

回答1:


Use

Navigator.push(context, new MaterialPageRoute(
  builder: (context) =>
     new MyHomePage())
  );

Instead of

Navigator.of(context).pushNamed('/home-page');
//or
Navigator.pushedName(context, '/home-page');



回答2:


Error says, Could not find a generator for route "home-page" in the _MaterialAppState.. As you are using NamedRoute (inferred from error message) and I think the problem is with route setting. Refer the example for route setting,

MaterialApp(
title: 'Named Routes Demo',
initialRoute: '/',
routes: { //route setting
  '/': (context) => FirstScreen(),
  '/home-page': (context) => HomePage(), //you should have something like this.
},

)




回答3:


Try it

  Navigator.push(context, new MaterialPageRoute(builder: (context) =>new PageName())



回答4:


This message tells, that in route list, the route you search aren't listed. So, check if in your MaterialApp->routes have your specified route.




回答5:


You need to define the route in specific dart file from where you want to jump to next screen. In your case for example there are three screens : 1. mainScreen.dart 2.loginScreen.dart 3.TabScreen.dart

Now you may have defined route for Loginscreen and TabScreen inside mainscreen.dart like :

routes : <String, WidgetBuilder>{
'/login' : (BuildContext context)=> LoginScreen()
'/tab' : (BuildContext context)=> TabScreen()
}

and you are trying to jump from LoginScreen to TabScreen but you haven't defined the route for TabScreen inside LoginScreen.dart

Please make Sure you have defined route for TabScreen inside LoginScreen :

routes : <String, WidgetBuilder>{
'/tab' : (BuildContext context)=> TabScreen()
}


来源:https://stackoverflow.com/questions/52405521/could-not-find-a-generator-for-route-home-page-in-the-materialappstate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!