如何在导航到视图时将数据发送到控制器

How to send Data to a controller while navigating to a view

我有这条路线

Get.toNamed('/community/discussion/${controller.community.topicId}}');

导航到视图时,我想将路由参数 topicId 传递给视图的控制器,以便它用作文档 ID 并加载将流式传输到视图的内容。

如何在流式传输视图内容之前将此参数发送到控制器?

我猜你没有按照预期的方式使用 Get.toNamed

函数 toNamed 接受一个 dynamic 参数 arguments,您可以将其用于您的目的。

在您的源屏幕中:

Get.toNamed<void>("/community/discussion/",
    arguments: {"topicId": controller.community.topicId});

在您的目标屏幕中:

var topicId = Get.parameters['topicId'];

然后您可以轻松地将主题 ID 设置到您的控制器中:

controller.topicId = topicId;

class YourController extends GetxController {
   int? topicId;
}

如果您要传递的内容已经存在于 GetX class 中,看起来确实如此,那么您根本不需要传递它。 GetX class 中的任何内容都可以从任何地方使用 Get.find...

访问

只需在下一页使用

访问它
Get.find<WhateverThisClassIsCalled>().community.topicId