在flutter web中,如何通过完整地址打开某个页面?
场景是:
- 打开一个教室页面路由,例如
await Navigator.pushNamed(context, "classroom/detail/$id", arguments: {"data": someData});
- 课堂页面打开,地址栏将包含类似的 URL
http://localhost/myschoolweb/classroom/detail/1
- 我按下
F5
刷新页面-> ERROR 404 - 打开新标签并访问上述地址-> ERROR 404
这是我的onGenerateRoute
代码:
onGenerateRoute: (RouteSettings settings) {
List<String> routes = settings.name?.split("/") ?? [];
final routeName = routes.isNotEmpty ? routes.first : null;
final routeSub1 = routes.length > 1 ? routes[1] : null;
final routeSub2 = routes.length > 2 ? routes[2] : null;
final args = {
...(settings.arguments as Map<String, dynamic>? ?? {}),
"sub1": routeSub1,
"sub2": routeSub2,
};
Widget page = const SplashPage();
switch (routeName) {
case ROUTE_CLASS: page = ClassPage(args); break;
// other routes ...
}
Future.microtask(() => FocusScope.of(context).requestFocus(FocusNode()));
return MaterialPageRoute(
settings: settings,
builder: (context) {
// other scripts ...
return page;
}
);
},
我怀疑它会被翻译成classroom/detail/1/index.html
服务器上当然不存在的?
ps:我使用这个库来简化网址:https ://pub.dev/packages/url_strategy