12

I'm using the following in my demo project for routes:

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

And trying to navigate home screen using the following code:

onPressed: () {
    debugPrint("Hello button is clicked");

     Navigator.of(context)
               .pushReplacementNamed('/HomePage');
 },

But When my button clicked I'm getting the following Exception:

════════ Exception caught by gesture ═══════════════════════════════════════════════════════════════
The following assertion was thrown while handling a gesture:
Could not find a generator for route RouteSettings("/HomePage", null) in the _WidgetsAppState.

Generators for routes are searched for in the following order:
 1. For the "/" route, the "home" property, if non-null, is used.
 2. Otherwise, the "routes" table is used, if it has an entry for the route.
 3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".
 4. Finally if all else fails onUnknownRoute is called.
Unfortunately, onUnknownRoute was not set.
When the exception was thrown, this was the stack: 
#0      _WidgetsAppState._onUnknownRoute.<anonymous closure> (package:flutter/src/widgets/app.dart:772:9)
#1      _WidgetsAppState._onUnknownRoute (package:flutter/src/widgets/app.dart:785:6)
#2      NavigatorState._routeNamed (package:flutter/src/widgets/navigator.dart:1625:22)
#3      NavigatorState.pushReplacementNamed (package:flutter/src/widgets/navigator.dart:1690:35)
#4      _RegisterPage.build.<anonymous closure> (package:oricon/register.dart:231:42)

I have already checked below Stack-overflow links

If you need more information, please do let me know.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Goku
  • 9,102
  • 8
  • 50
  • 81

6 Answers6

15

try this

 Navigator.of(context, rootNavigator: true).pushNamed("/HomePage");
Chris
  • 1,641
  • 1
  • 16
  • 17
6

Are you implementing to the main Class that's receiving your paths?

Here's an example:

void main() => runApp(MyApp()); //as you can see here, it is the main widget

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'MyApp',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: MyHomePage(title: 'Home Page'),
      routes: {
        'profile': (context) => ProfilePage(),
         /*Here's where you receive your routes, and it is also the main widget*/
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

Also, I have to add that your question helped me find what was going wrong with my code. Thanks!!!

Manbbo
  • 97
  • 7
6

You can use instead of:

Navigator.of(context)
           .pushReplacementNamed('/HomePage');

Use this:

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

As you can see in this answer, this worked for me

Askaredox
  • 79
  • 1
  • 6
0

You can search for MaterialApp in your code (Do not consider Plugin code), and also give arguments if you are using pushName navigator that can avoid the null problem.

Please check this two MaterialApp .

YuvrajsinhJadeja
  • 1,383
  • 7
  • 23
Arjun Ranjith
  • 209
  • 1
  • 5
0

I had navigation drawer and one of my route was going to a route which had another MaterialApp() Widget and after routing to that page if i route back to any other page it will through this exception.

because the context was changed to new material app and i was trying to find that route inside new material app but obviously it was not present there.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30933849) – RaBaKa 78 Feb 04 '22 at 05:58
0

you didn't register your route in main.dart file