Flutter on web,使用 url 中的查询参数重定向到初始路由

Flutter on web, using query params in url redirects to inital route

我在将 Flutter 用于网页时遇到了一个非常奇怪的问题。我需要处理来自外部来源的查询参数,该参数通过例如 /page?param=1

访问我的网站

我有一个超级简单的 flutter 项目设置:

import 'package:client/screens/screens.dart';
import 'package:flutter/material.dart';
import 'package:url_strategy/url_strategy.dart';

void main() {
  setPathUrlStrategy();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        "/": (context) => HomeScreen(),
        "/page": (context) => PageScreen(),
      },
    );
  }
}

当转到“/”或“/page”时,它工作正常。但是只要我去“/page?param=1”。我收到以下错误:

The following message was thrown:
Could not navigate to initial route.
The requested route name was: "/page?param=1"
There was no corresponding route in the app, and therefore the initial route specified will be
ignored and "/" will be used instead.

Flutter 看不到查询参数是什么吗?是 web 101 在做这样的事情,我一定是做错了什么,我就是找不到答案。

尝试在 MaterialApp 中使用 onGenerateRoute 回调,例如:

   onGenerateRoute: (RouteSettings settings) {
    Widget? pageView;
    if (settings.name != null) {
      var uriData = Uri.parse(settings.name!);
      //uriData.path will be your path and uriData.queryParameters will hold query-params values
 
      switch (uriData.path) {
        case '/page':
          pageView = PageScreen();
          break;
        //....
      }
    }
    if (pageView != null) {
      return MaterialPageRoute(
          builder: (BuildContext context) => pageView!);
    }
  },