英文:
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
。
祝你编码愉快
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论