颤振:在_WidgetsAppState中找不到路由路由设置("/HomePage",null)的生成器



我在演示项目中对路由使用以下方法:

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

并尝试使用以下代码导航主屏幕:

onPressed: () {
debugPrint("Hello button is clicked");
Navigator.of(context)
.pushReplacementNamed('/HomePage');
},

但是当我的按钮单击时,我收到以下异常:

════════ 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)

我已经在下面检查了堆栈溢出链接

  • 在_WidgetsAppState中找不到路由路由设置("/",空(的生成器
  • 如何在颤振中使用路由导航到主页面以外的页面?
  • 在_MaterialAppState中找不到路由"主页"的生成器
  • 为什么当我使用 pushNamedReplace 而不是 pushReplace 时它不起作用?

如果您需要更多信息,请告诉我。

试试这个

Navigator.of(context, rootNavigator: true).pushNamed("/HomePage");

您是否正在向接收路径的主类实现?

下面是一个示例:

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

另外,我必须补充一点,您的问题帮助我找到了代码中的问题。谢谢!!!

您可以使用代替:

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

使用这个:

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

正如你在这个答案中看到的,这对我有用

您可以在代码中搜索 MaterialApp(不要考虑插件代码(,如果您使用的是可以避免空问题的 pushName 导航器,也可以给出参数。

请检查这两个材料应用程序

我有导航抽屉,我的一条路线是去一条有另一个 MaterialApp(( 小部件的路线,在路由到该页面后,如果我路由回任何其他页面,它将通过此异常。

因为上下文已更改为新材料应用程序,我试图在新材料应用程序中找到该路线,但显然它不存在。

您没有在 main.dart 文件中注册您的路由

最新更新