flutter web 无法访问url

Flutter web cannot visit url

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

场景是:

  1. 打开课堂页面路由 like await Navigator.pushNamed(context, "classroom/detail/$id", arguments: {"data": someData});
  2. 课堂页面已打开,地址栏将包含 URL,如 http://localhost/myschoolweb/classroom/detail/1
  3. 我按F5刷新页面->错误404
  4. 打开新标签并访问上述地址 -> 错误 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: 我使用这个库来简化网络 url: https://pub.dev/packages/url_strategy

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

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

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