2

在flutter web中,如何通过完整地址打开某个页面?

场景是:

  1. 打开一个教室页面路由,例如 await Navigator.pushNamed(context, "classroom/detail/$id", arguments: {"data": someData});
  2. 课堂页面打开,地址栏将包含类似的 URLhttp://localhost/myschoolweb/classroom/detail/1
  3. 我按下F5刷新页面-> ERROR 404
  4. 打开新标签并访问上述地址-> 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

4

1 回答 1

0

以防万一有人需要它,我已经使用 .htaccess 文件在 apache 服务器上解决了这个问题。

RewriteEngine On
RewriteBase /
RewriteCond $1 !^(index\.html|assets|robots\.txt|favicon\.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.html [L]

因此,任何 URL 现在都将默认为 index.html。

于 2021-12-15T05:17:25.987 回答