使用go_router软件包时热重新加载的未知行为
我在项目中使用 go_router 。我有一个带有Gorouter实例的单独文件,其中包括所有路由(家,登录,注册)。然后,我添加了用Cubit的身份验证。因此,我必须将Gorouter实例修改为使用Auth Cubit接收的函数,并将其重定向到适当的路线。
一切似乎都还好,但后来我意识到了一些东西。例如,如果我在登录路线中并将寄存器推到堆栈并修改寄存器页面并保存文件,则热加载将带我回到登录。因此,每次我想对寄存器页面进行一些更改时,我都会返回登录路线,然后手动返回寄存器查看我的更改。
这是一个示例:
ps:我刚刚开始使用 go_router package所以也许我正在做一些错误的事情。
I was using go_router in a project. I had a separate file with an instance of GoRouter with all the routes (home, login, register). Then, I added authentication with a cubit. So I had to modify my GoRouter instance to a function that received with auth cubit and use it to redirect to the appropriate route.
Everything seemed alright but then I realized something. For example, if I was in the login route and push register to the stack and modify the register page and save the file, the hot reload would take me back to login. So every time I want to make some changes to the register page, I would go back to the login route and then manually go back to the register to see my changes.
Here is an example:
PS: I just started using the go_router package so maybe I am doing something incorrectly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
该解决方案简单
gorouter 存储路线的状态。如果您创建了 gorouter 的新实例,则它会失去状态并导航到初始路线。
为了解决这个问题,我创建了 gorouter 的实例,并将其存储为单例或全局实例。
The solution is simple
GoRouter store state of your routes. If you create new instance of GoRouter then it lose state and navigate you to initial route.
To solve this problem I create Instance of GoRouter and store it as Singleton or as Global instance.
只需将路由器标记为 static ,就可以在每次初始化 youterapp.router()中使用相同的实例,
并且在portialapp.router()中:
Simply mark your router as static, so that it uses the same instance when you initialize it everytime in MaterialApp.router()
And, In MaterialApp.router():
您应该将创建的路由类定义为最终,并将其提供给材料应用程序。它对我有用。
最终_appRouter = navigatorroutes();
routerconfig:_approuter.router,
我的navigatorroutes类是这样
You should define the route class you created as final and give it to Material App. It worked for me.
final _appRouter = NavigatorRoutes();
routerConfig: _appRouter.router,
My NavigatorRoutes class is like this
我做了一个可以正常工作的解决方法。
您可以使用示例代码查看模板存储库。
它由飞溅,入职,标志以及带有纳维尔和子女页面的家庭组成。
也可以与 redirect 一起使用 authstate changeNotifier 。
https://github.com/christer.com/christer-christer-muntean/go_router_router_with_with_with_with_navbar <
/> > APP视频预览此处: https://www.utvikler.app/post/post/spar-tid-tid-tid-gog-resser--resefektiv-navigering-inavigering-i-i-i-i-flutter-i-flutter-med-go_router-med-go_router-mot-go_router-motforsk----- V%C3%A5RT-GITHUB-REPO
I've made a workaround that works ok.
You can check out the template repo with example code.
It consists of Splash, OnBoarding, SignIn, and Home with NavBar and child pages.
There is also use of redirect together with an AuthState ChangeNotifier.
https://github.com/Christer-Muntean/go_router_with_navbar
You can check out the app video preview here: https://www.utvikler.app/post/spar-tid-og-ressurser-effektiv-navigering-i-flutter-med-go_router-utforsk-v%C3%A5rt-github-repo
如果您使用Authstate提供商检查用户是否已登录并重定向他,则使用GO_ROUTER非常棘手。
当我开始使用Gorouter的重定向方法时,它似乎真的很容易管理,但是后来我意识到热重新加载总是将我重定向到InitialRoute'/'。
在我的项目中,我使用RiverPod检查Authstate(
Ref.Watch(CurrentUserAccountProvider)
)。但是要在Gorouter配置中使用此代码,配置必须是动态的(vs static)。每当您的应用程序重建时( - &gt;重定向到“/”),这是我的解决方案。我很狂热,所以我不知道这是一个好习惯,有经验的用户可以说,但至少对我有用。
Gorouter配置文件:
然后,我创建了一个小部件来管理Authstate的重定向:
Checkauthwidget:
这是我的主要:
我希望这可以帮助别人,请告诉我我是否做错了什么:)
If you are using an authState provider to check if user is logged in or not and redirect him, using go_router is quite tricky.
When I started using the redirect method of GoRouter it seems really easy to manage, but then I realized that the hot reload always redirected me to the initialRoute '/'.
In my project I use riverpod to check for the authState (
ref.watch(currentUserAccountProvider)
). But to use this code in the GoRouter config, the config must be dynamic (vs static). And it result of re-initializing GoRouter config each time your app is rebuilt (-> redirect to "/")So here is my solution. I am quite new to Flutter so I don't know if it's a good practice or not, experienced users could say, but at least it works for me.
GoRouter config file :
Then I created a widget to manage the redirection following the authState:
CheckAuthWidget :
Here is my main :
I hope this could help someone else, and please tell me if I am doing something wrong :)
尝试这样实施:
Try to implement like this:
通过遵循代码,您可以实现欲望输出。
By following code you can achieve desire output.
如果其他人仍然遇到问题,那么这就是解决方案
If someone else still getting issues then here is the solution