简体   繁体   中英

How to manage Flutter web URL routes using the GetX package?

I am developing a Flutter app for iOS, Android, and Web. I decided to use the GetX package because it makes things much easier, but I am stumped on how to handle Web URL/address bar navigation. For example, one of my screens shows details about an event. Navigating there through the app works fine, but what if I paste in a link to the event in the browser? My GetX EventController manages a _selectedEvent event which is how I know which event to display the details for. However, if I use a URL link, my _selectedEvent event is never updated and the details screen won't show the correct event.

Currently I set _selectedEvent in my homepage when the user clicks on the event to navigate to the details screen. However, if the user navigates through the address bar in a browser, I cannot update _selectedEvent . I don't think I can use any of the state managers in GetX because I am not updating widgets, so I am not sure where to update my _selectedEvent .

How should I handle address bar navigation?

Hopefully my explanation makes sense. I am still trying to learn Flutter.

Try using GetX named routes with dynamic URL links as described here . For example, you could have a URL like 'https://.../events?id=1234' for a specific event, you can obtain the ID with Get.parameters['id'] in your controller and show the event details based on this id.

So I found a somewhat solution if anyone else stumbles onto this question. This link has a good, easy-to-follow description and implementation of routing and navigating with Flutter. It doesn't use GetX specifically to do routing, but I just used GetPageRoute instead of MaterialPageRoute and GetX navigation instead of the Flutter Navigator to still benefit from GetX controllers and bindings. I still have problems with the browser back button, but I think that is a Flutter problem, no a problem of my specific project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM