5

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.
Code Hunter
  • 10,075
  • 23
  • 72
  • 102
  • Code Hunter, you need to share your code. I think I know the answer but I need to confirm how you are using Navigator.push – F-1 Sep 19 '18 at 13:07

5 Answers5

28

Use

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

Instead of

Navigator.of(context).pushNamed('/home-page');
//or
Navigator.pushedName(context, '/home-page');
F-1
  • 2,887
  • 20
  • 28
  • This was a solution for me; however push doesn't seem to support an object as an argument? any idea to circumvent that limitation? – cdsaenz May 14 '19 at 05:03
  • 2
    @cdsaenz did you figure it out? Did you mean you wanted to pass an object through the MyHomePage() method e.g new MyHomePage(fooObj) so that it could be used there? – F-1 May 29 '19 at 14:10
  • not at my computer right now but I remember creating a class to hold the parameters, so it was something like this: `class PageParameters { final num id; final String title; PageParameters(this.id,this.title); }` – cdsaenz May 29 '19 at 18:54
  • `class RequestEditPage extends StatelessWidget { final PageParameters data; RequestEditPage({this.data}); ` – cdsaenz May 29 '19 at 18:57
  • 1
    Sorry the bad formatting, but yes, basically you add this object in the page widget and you send the data as a parameter to the new page in the push operation. I got my inspiration here [link](https://medium.com/flutter-community/simple-ways-to-pass-to-and-share-data-with-widgets-pages-f8988534bd5b) – cdsaenz May 29 '19 at 19:00
  • Since your class has those parameters in your build method you should be able to reference your object using widget.object eg. widget.data, widget.title etc. – F-1 May 30 '19 at 10:00
  • 5
    Any explanation to why? – Gabriel Ziegler Oct 18 '19 at 10:16
  • 1
    Add an explanations for this... why? – Rahul Kushwaha Feb 25 '21 at 08:19
2

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.

Dumitru Boaghi
  • 183
  • 2
  • 4
0

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.
    },
  )
Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65
Dinesh Balasubramanian
  • 20,532
  • 7
  • 64
  • 57
0

Try it

  Navigator.push(context, new MaterialPageRoute(builder: (context) =>new PageName())
Muhammad Ashraf
  • 3,323
  • 2
  • 12
  • 19
0

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()
}