簡體   English   中英

Flutter Web Url 路由在真實域上不起作用

[英]Flutter Web Url Route Doesn't Work on Real domain

我正在嘗試使用 url 獲取參數並將該參數分配給 web 文件中的變量。

例如,我的域是 example.com,在這個網站上我需要一個用戶 ID。 我想制作 example.com/?id=123 並獲取 123 id 並給出變量 123 值

在 flutter web 設備中它可以工作,但是當我托管此文件時,它不能在真實域上工作。 flutter 給了我一個

Could not navigate to initial route.
The requested route name was: "/?id=sezen@gmail.com"
There was no corresponding route in the app, and therefore the initial route specified will be
ignored and "/" will be used instead.

這是我的代碼

void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      setPathUrlStrategy();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
    
  runApp(const MyApp());
}

String myurl = Uri.base.toString(); //get complete url

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  // This widget is the root of your application.
  @override
  void initState() {
    super.initState();
    getParams();
  }

  @override
  Widget build(BuildContext context) {
    getParams();

    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'QR Numaratör',
      home: MyHomePage(id: mail),
    );
  }
}

void getParams() {
  var uri = Uri.dataFromString(window.location.href);
  Map<String, String> params = uri.queryParameters;
  var origin = params['id'];
  mail = origin;
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.id}) : super(key: key);
  String? id = Uri.base.queryParameters["id"];
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

在您的 MaterialApp 小部件中,您需要指定 onGenerateRoute 參數。 像這樣的東西:

return MaterialApp(
  debugShowCheckedModeBanner: false,
  title: 'QR Numaratör',
  home: MyHomePage(id: mail),
  onGenerateRoute: (settings) {
            switch (kIsWeb? Uri.parse(settings.name).path : settings.name) {
              case'/':
                  Map args = settings.arguments as Map;
                  if(kIsWeb && args == null) {
                     args=Uri.parse(settings.name).queryParameters;
                   }
                  return MaterialPageRoute(builder: (_) => MyHomePage(id: args[id])); 
   }
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM