Gok*_*oku 8 android dart flutter
我在我的演示项目中使用下面的路线
routes: <String, WidgetBuilder>{
'/HomePage': (BuildContext context) => HomePage()
},
Run Code Online (Sandbox Code Playgroud)
并尝试使用以下代码导航主屏幕
onPressed: () {
debugPrint("Hello button is clicked");
Navigator.of(context)
.pushReplacementNamed('/HomePage');
},
Run Code Online (Sandbox Code Playgroud)
但是当我点击按钮时,我得到了低于异常
???????? 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)
Run Code Online (Sandbox Code Playgroud)
我已经检查了下面的 Stack-overflow 链接
如果需要更多信息,请告诉我。提前致谢。您的努力将不胜感激。
Chr*_*ris 19
尝试这个
Navigator.of(context, rootNavigator: true).pushNamed("/HomePage");
Run Code Online (Sandbox Code Playgroud)
小智 6
您是否正在实施接收路径的主类?
这是一个例子:
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();
}
Run Code Online (Sandbox Code Playgroud)
另外,我必须补充一点,您的问题帮助我找到了我的代码出了什么问题。谢谢!!!
小智 6
您可以使用代替:
Navigator.of(context)
.pushReplacementNamed('/HomePage');
Run Code Online (Sandbox Code Playgroud)
用这个:
Navigator.push(context, new MaterialPageRoute(
builder: (context) => new MyHomePage())
);
Run Code Online (Sandbox Code Playgroud)
正如你在这个答案中看到的,这对我有用
| 归档时间: |
|
| 查看次数: |
11051 次 |
| 最近记录: |