[英]How to manage Flutter web URL routes using the GetX package?
I am developing a Flutter app for iOS, Android, and Web.我正在为 iOS、Android 和 Web 开发 Flutter 应用程序。 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.
我决定使用 GetX 包,因为它使事情变得更容易,但我对如何处理 Web URL/地址栏导航感到困惑。 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.我的 GetX EventController 管理一个
_selectedEvent
事件,这就是我知道要显示详细信息的事件的方式。 However, if I use a URL link, my _selectedEvent
event is never updated and the details screen won't show the correct event.但是,如果我使用 URL 链接,我的
_selectedEvent
事件永远不会更新,详细信息屏幕也不会显示正确的事件。
Currently I set _selectedEvent
in my homepage when the user clicks on the event to navigate to the details screen.目前,当用户单击事件以导航到详细信息屏幕时,我在主页中设置了
_selectedEvent
。 However, if the user navigates through the address bar in a browser, I cannot update _selectedEvent
.但是,如果用户通过浏览器中的地址栏导航,我无法更新
_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
.我不认为我可以在 GetX 中使用任何状态管理器,因为我没有更新小部件,所以我不确定在哪里更新我的
_selectedEvent
。
How should I handle address bar navigation?我应该如何处理地址栏导航?
Hopefully my explanation makes sense.希望我的解释有意义。 I am still trying to learn Flutter.
我仍在努力学习 Flutter。
Try using GetX named routes with dynamic URL links as described here .尝试使用动态URL链接所描述的getX命名路由在这里。 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.
例如,对于特定事件,您可以有一个类似“https://.../events?id=1234”的 URL,您可以在控制器中使用 Get.parameters['id'] 获取 ID 并显示该事件基于此 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.
这个链接有一个很好的、易于遵循的描述和使用 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.它没有专门使用 GetX 来进行路由,但我只是使用了
GetPageRoute
而不是MaterialPageRoute
和 GetX 导航而不是 Flutter Navigator
以仍然受益于 GetX 控制器和绑定。 I still have problems with the browser back button, but I think that is a Flutter problem, no a problem of my specific project.我的浏览器后退按钮仍然有问题,但我认为这是一个 Flutter 问题,不是我的特定项目的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.