防止在Flutter web中重新初始化Provider

huangapple go评论59阅读模式
英文:

Prevent re-initialization of Provider in Flutter web

问题

我目前遇到了一个关于我的Flutter web应用的问题。当我在新标签页中打开一个页面时,提供程序会重新初始化。这种行为类似于传统Web应用程序中页面刷新的方式。因此,我丢失了我的保存状态。有没有办法在不依赖本地存储的情况下保存它,并在所有标签页中访问它?请查看我用来在新标签页中打开页面的代码:

html.window.open('/#${Result.routeName}', "_blank");
英文:

I am currently experiencing an issue with my Flutter web app. When I open a page in a new tab, the providers reinitialize. This behavior is similar to how a page refresh works in a traditional web application. As a result, I have lost my saved state. Is there any way to save it without relying on local storage and access it across all my tabs? Please find below the code I am using to open the page in a new tab:

html.window.open('/#${Result.routeName}', "_blank");

答案1

得分: 1

作为一种替代的状态管理包,bloc 提供了一个解决方案,在 hydrated_bloc 包下可以找到你需要的东西。

但是在提供者方面,也许你可以使用 BroadcastChannel 类来解决问题,但创建一个本地存储会更加简单和有效。

你可以使用以下方式创建一个本地存储:

flutter_secure_storage:

或者

shared_preferences:

然后,你可以简单地创建一个方法来写入你希望在路由后恢复的状态。在新页面上,只需恢复这些状态并删除恢复存储。

这可能是最简单的解决方案,或者你也可以考虑迁移到 bloc

祝你编码愉快 防止在Flutter web中重新初始化Provider

英文:

As an alternative state management package, bloc offers a solution under hydrated_bloc package what you are looking for.

But on provider side maybe you can use BroadcastChannel class to solve that but creating a local storage would be much easier and effective.

You can create a local storage with

flutter_secure_storage: 

or 

shared_preferences:

Then you can simply create a method to write which states you want to recover after routing. After it on the new page just recover those states and delete that recovery-storage.

That will be the easiest solution I guess or you can move up to bloc.

Have a great coding session 防止在Flutter web中重新初始化Provider

huangapple
  • 本文由 发表于 2023年6月13日 03:47:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定