在颤振网页上重新加载页面时"not found"



当我在Chrome上重新加载页面时,在颤振网络上,我得到文本"未找到"。我该如何解决它?这是我的 main.dart 代码。我还注意到,要直接进入页面,我必须在 url 中插入一个哈希符号 (#(,如下所示:"http://127.0.0.1:8080/#/homepage"。有没有办法删除它?

class MyApp extends StatefulWidget {
const MyApp({Key key}): super(key: key);
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
// This widget is the root of your application.
@override
void initState() {
html.window.history.pushState(null, "Home", "/");
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
initialRoute: "/",
theme: ThemeData(
primarySwatch: Colors.blue,
fontFamily: 'GoogleSansRegular'
),
routes: {
"/": (context) => HomePage(),
"/homepage": (context) => HomePage(),
"/secondPage": (context) => SecondPage()
},
);
}
}

要删除 URL 中的 #,您必须切换以设置 UrlStrategy,就像这里描述的那样:https://docs.flutter.dev/development/ui/navigation/url-strategies

长话短说:将此包(https://pub.dev/packages/url_strategy(添加到pubspec.yaml,并在main方法中调用setPathUrlStrategy((:

import 'package:url_strategy/url_strategy.dart';
void main() {
// Here we set the URL strategy for our web app.
// It is safe to call this function when running on mobile or desktop as well.
setPathUrlStrategy();
runApp(MyApp());
}

也许它也可以解决你的另一个问题。如果没有,那么我认为使用AutoRoute软件包是个好主意:https://pub.dev/packages/auto_route

相关内容

  • 没有找到相关文章

最新更新