无法使用 GetPage 读取查询参数 – Flutter

huangapple go评论62阅读模式
英文:

Unable to read Query Parameters Using GetPage - Flutter

问题

I'm using GetMaterialApp.router from GetX as I have used it to handle navigation in my app, but surprisingly it does not provide the same set of parameters as MaterialApp.router.

Nevertheless, I added routes using getPages. I have been trying to access Query parameters but it does not work. I'm able to access Path parameters using Get.parameters but it does not work for Query params.

Here's my code, and I intend to take test1 as a query parameter, and I'm getting null;

 GetPage(
          name: '/:session/:test',
          page: () {
            return Scaffold(
              appBar: AppBar(title: Text('${Get.parameters['session']}')),
              body: Column(
                children: [
                  Text('${Get.parameters['test']}'),
                  Text('${Get.parameters['test1']}'),
                ],
              ),
            );
          },
        ),

I'm trying to open the app using a dynamic link and for path params, it works fine but it does not seem to work for query params.

英文:

I'm using GetMaterialApp.router from getx as I have used it to handle navigation in my app, but surprisingly it does not provide the same set of parameters as MaterialApp.router.

Nevertheless, I added routes using getPages. I have been trying to access Query parameters but it does not work. I'm able to access Path parameters using Get.parameters but it does not work for Query params.

Here's my code, and I intent to take test1 as query parameter, and I'm getting null;

 GetPage(
          name: '/:session/:test',
          page: () {
            return Scaffold(
              appBar: AppBar(title: Text('${Get.parameters['session']}')),
              body: Column(
                children: [
                  Text('${Get.parameters['test']}'),
                  Text('${Get.parameters['test1']}'),
                ],
              ),
            );
          },
        ),

I'm trying to open the app using a dynamic link and for path params it works fine but it does not seem to work for query params.

答案1

得分: 0

根据GetX的创建者说,Get.parametersGet.arguments 是临时的,必须分配给一个变量。参考链接

> 从查询中读取

//路径  /:session/:test?param1=value1&param2=value2

final queryParams = Get.parameters;
final param1 = queryParams['param1'];
final param2 = queryParams['param2'];

> 从路径中读取

//路径 /session/param1/param2

final param1 = Get.parameters['param1'];
final param2 = Get.parameters['param2'];
英文:

According to the creator of Getx, Get.parameters and Get.arguments are ephermal and have to be assigned to a variable. Reference

> Read from query

//Path  /:session/:test?param1=value1&param2=value2

final queryParams = Get.parameters;
final param1 = queryParams['param1'];
final param2 = queryParams['param2'];

> Read from path

//Path /session/param1/param2

 final param1 = Get.parameters['param1'];
 final param2 = Get.parameters['param2'];

答案2

得分: 0

我开始使用 GetMaterialApp.router,因为之前我曾使用 MaterialApp.router 处理 query 参数,当用户点击链接打开应用程序时。这样我可以读取 path 参数,但是尝试访问 query 参数时得到了 null

在未能找到任何解决方案后,我决定回到使用 GetMaterialApp,看看它是否提供了像 GetMaterialApp.router 构造函数一样的路由选项,结果我的问题得到了解决。

GetMaterialApp(
  initialRoute: '/',
  getPages: [
    GetPage(
      name: '/',
      page: () {
        return const OdooAuthWrapper();
      },
    ),
    GetPage(
      name: '/signup',
      page: () {
        return  AddNewPasswordPage(token: Get.parameters['token'],);
      },
    ),
  ],
),

GetMaterialApp.router 提供了更复杂的路由配置选项,对于某些情况可能更合适,但在我的情况下,使用 GetMaterialApp 就足够了。

英文:

I started off by using GetMaterialApp.router as I had previously used MaterialApp.router to handle query parameters, when user taps on a link to open the app. This way I was able to read path parameters, but was getting null, when trying to access query parameters.

After failing to find any solution, I decided to shift back to GetMaterialApp and see if it offers routing options just like the GetMaterialApp.router constructor, and with that my problem was solved.

GetMaterialApp(
        initialRoute: '/',
        getPages: [
          GetPage(
            name: '/',
            page: () {
              return const OdooAuthWrapper();
            },
          ),
          GetPage(
              name: '/signup',
              page: () {
                return  AddNewPasswordPage(token: Get.parameters['token'],);
              }),
        ],
        // home:  getPage(),
      ),

GetMaterialApp.router has more complex routing configurations to offer, and for some it might make more sense to use it, but in my case GetMaterialApp was enough.

huangapple
  • 本文由 发表于 2023年5月11日 18:12:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定