getx
getx copied to clipboard
Using route parameters with Get.back()
When using route parameters, I am able to move through pages by changing the route parameters however if I try to return to the previous page my route parameters do not seem to be passed and therefore I cannot use them within my controller or store them as a variable to be used later.
Please find reproduction code at: https://github.com/shaniemartin/getx_navigation_demo This is a simple reproduction of the issue. Here you move between articles passing different id parameters to the same screen in order to display a different article. This works fine when using Get.toName(/article/:id) however I get issues when using Get.back() to display the previous article.
To reproduce the issue
- Run the app
- Click next article
- Note that the printout inside the debug console and the screen have matching ID numbers.
- Do this a number of times and you will see this working as expected.
- Click the back button in the app bar
- Note now that the printout is displaying a different ID to the console.
- Even though the screen updates correctly the variable in the controller is not getting updated as it should as I believe that no route parameters are getting passed back with Get.back().
Expected behaviour: When using Get.back(), route parameters should be passed in a similar way to Get.toNamed so that they can be used inside the controller or saved as a variable to be used later.
Flutter version: 2.0.3 Getx version: 4.14 Device: Pixel 3 Android emulator
Minimal reproduce code to be found at : https://github.com/shaniemartin/getx_navigation_demo
If I understand what you mean, you are trying to pass a parameter to an earlier route. You should use the result of Get.back, not arguments to do so.
https://github.com/jonataslaw/getx/blob/master/documentation/en_US/route_management.md#navigation-without-named-routes
Thanks for getting back to me, however I'm not sure if I explained the issue correctly. I have article pages containing lots of information and want to move between articles. I am using dynamic urls's to retrieve the id for each article and pass the correct information back from the server to the view. This works fine in the forward direction (Get.toNamed) as I can use route parameters and Get.parameters(['id']) to fetch the correct article id and pass that to the server. However in the backwards direction (Get.back), even though you are moving between url's, Get.parameters does not work. Therefore I cannot use this method to retrieve the information for the previous page.
In summary I would like to move from one article to another, and when the user hits back I would like them to see all of the information for the previous article. What is the best way to achieve this with Getx and GetxControllers.
I think I just ran into this and the documentation does need clarification. The documentation says
on other screen, send a data for previous route:
Get.back(result: 'success');
And use it:
ex:
if(data == 'success') madeAnything();
.. but it's not clear how to access result in the parent controller that called Get.to(), you would have to start looking at the method signatures to see what's available.
The result of Get.back can be accessed by either awaiting the result of Get.to or chaining its future...
Get.to(() => MyScreen(), arguments: {})!
.then((value) => {
print('Navigated back, back value is ' + value.toString())
}
);
// OR
var backValue = await Get.to(() => MyScreen(), arguments: {});
print('back! ' + backValue.toString());
// in this case your method must be async to use await.
I don't think anybody that commented got the issue right. I am now presented with the same problem as the creator:
When navigating to /route/:id
, and then calling Get.back()
, the system returns to a URL like /route/null
. Then of course you have no access to the parameters as before.
This is not a feature request or enhancement, is a bug and I don't know how to resolve it.
@shaniemartin did you manage to make this work somehow? It is driving me insane.
EDIT: For anyone that encounters this problem: Apparently parameters just disappear as the route enters the scene. The solution for this is just to save parameters in any variable onInit
, and you are good to go. The parameters will stay in the variables as long as the controller lives.
I also met a similar issue.It seems when pressing chrome browser back button, the Get.parameters still use previouse route arguments.
name: /entrance/:page
and here is my log,print their routes and Get.arguments (show as {page: xxx} )
[DebugTool] : <currentPage> Route["/entrance/2"]->| 2
[DebugTool] : <currentPage params> Route["/entrance/2"]->| {page: 2}
[DebugTool] : <important> Route["/entrance/2"]->| DynamicConfig.blogContentWidth initialized ...
[DebugTool] : <entrance> Route["/entrance/2"]->| /entrance/2
[DebugTool] : <currentPage> Route["/entrance/2"]->| 2
[DebugTool] : <currentPage params> Route["/entrance/2"]->| {page: 2}
[DebugTool] : <important> Route["/entrance/2"]->| DynamicConfig.blogContentWidth initialized ...
[DebugTool] : <entrance> Route["/entrance/1"]->| /entrance/1
[DebugTool] : <currentPage> Route["/entrance/1"]->| 2
[DebugTool] : <currentPage params> Route["/entrance/1"]->| {page: 2}
and Route["/entrance/1"]->| {page: 2}
is wrong
@kuro7766 somewhere in the issues I caught that parameters are 'ephemeral', meaning that they are created once and then disappear from reference. So, you can't always do Get.paramaters
or Get.arguments
, but rather only once when the controller loads. The solution is just to store the value of Get.paramaters
or Get.arguments
inside a variable and you will have that same value available whenever you need it.
What's surprising here is the little support provided for such a small issue, which of course it is not documented anywhere.
I hope I'd help you.
Same problem here