我正在开发flutter web,并尝试运行演示代码getx上得到4.6.5,但当使用Get.toNamed()时,我得到了错误“Navigator.onGenerateRoute为空,但路由名为“/second/id=3”被引用.”然后我尝试get.to(),但URL没有改变,并重新加载页面,我无法返回到page 1以下是我的代码:
class MyApp extends StatelessWidget {
MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return GetMaterialApp.router(
getPages: [
GetPage(
participatesInRootNavigator: true,
name: '/first',
page: () => First()),
GetPage(
name: '/second/:id',
page: () => Second(),
),
GetPage(
name: '/third',
page: () => Third(),
),
],
debugShowCheckedModeBanner: false,
);
}
}
class First extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('page one'),
leading: IconButton(
icon: Icon(Icons.more),
onPressed: () {
Get.changeTheme(
context.isDarkMode ? ThemeData.light() : ThemeData.dark());
},
),
),
body: Center(
child: Container(
height: 300,
width: 300,
child: ElevatedButton(
onPressed: () {
Get.toNamed('/second/id=3');
},
child: Text('next screen'),
),
),
),
);
}
}
class Second extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('page two ${Get.parameters["id"]}'),
),
body: Center(
child: Container(
height: 300,
width: 300,
child: ElevatedButton(
onPressed: () {
Get.toNamed('/third');
},
child: Text('next screen'),
),
),
),
);
}
}
我是flutter网站的新手希望得到大家的帮助
2条答案
按热度按时间rqmkfv5c1#
不如这样做:
t3psigkw2#
谢谢大家,我用
Get.rootDelegate.toNamed()
解决了这个问题