Flutter, how to update reactive list declared in first screen controller from the second screen?

Viewed 70

I am using GetX in my project. Lets suppose I have two screens. Each screen have a button. The controller of first screen have observable list of objects. When a button in first screen is pressed, one of the object list is sent to second screen.

In second screen user changes the value of the data received and when save button is pressed, I want to update the observable list in first screen as well.

I know I can use Get.back() to send updated data back to first screen. I could do following in second screen

Get.back(result: [
    {"updatedObject": listDetails}
]);

And in first screen I could do

Get.to(() => SecondScreen(), arguments: [
   {"list": listDetails[index]}
]).then((result) {
    // result[0]["updatedObject"]  // use this to update list observable
});

My Question. Can I update list in first screen without navigating back to first screen when Save button is pressed.

I am pretty new with GetX and i am trying to learn it. Thanks

1 Answers

As long that when you navigate to the second screen using Get.to() then you can always access the data in the controller of the first screen using Get.find().

Here's a sample I tweaked it a little bit:

Get.to(() => SecondScreen(), arguments: 
   {"index": index},
)

On the controller on the second screen you can access(view/update) the data using the index.

@override
void onInit() {
   index = Get.arguments['index'];
   // View
   name = Get.find<FirstScreenController>().listDetails[index].name;
   print(name);
   // Update
   Get.find<FirstScreenController>().listDetails[index].name = "John";
}
Related