见官方解决方案:Configuring the URL strategy on the web
启用 flutter_web_plugins
dependencies:
flutter_web_plugins:
sdk: flutter
创建配置文件 lib/configure_nonweb.dart
void configureApp() {
// No-op.
}
创建配置文件 lib/configure_web.dart
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
void configureApp() {
setUrlStrategy(PathUrlStrategy());
}
在 lib/main.dart
中调用
import 'package:flutter/material.dart';
import 'configure_nonweb.dart' if (dart.library.html) 'configure_web.dart';
void main() {
configureApp();
runApp(MyApp());
}
运行项目即可