见官方解决方案:Configuring the URL strategy on the web

启用 flutter_web_plugins

  1. dependencies:
  2. flutter_web_plugins:
  3. sdk: flutter

创建配置文件 lib/configure_nonweb.dart

  1. void configureApp() {
  2. // No-op.
  3. }

创建配置文件 lib/configure_web.dart

  1. import 'package:flutter_web_plugins/flutter_web_plugins.dart';
  2. void configureApp() {
  3. setUrlStrategy(PathUrlStrategy());
  4. }

lib/main.dart 中调用

  1. import 'package:flutter/material.dart';
  2. import 'configure_nonweb.dart' if (dart.library.html) 'configure_web.dart';
  3. void main() {
  4. configureApp();
  5. runApp(MyApp());
  6. }

运行项目即可