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

Issue

I have this route

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

While navigating to the view, I want to pass the route parameter topicId to the view’s controller so that it uses as document ID and load the content which will be stream to the view.

How can I send this parameter to the controller before the view’s content is streamed?

Solution

I guess you’re not using Get.toNamed the way it is supposed to.

Function toNamed takes a dynamic parameter arguments that you can use for your purpose.

In your source screen:

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

In your destination screen:

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

Then you can easily set the topic id into your controller:

controller.topicId = topicId;

class YourController extends GetxController {
   int? topicId;
}

Answered By – Nicola Gallazzi

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.