Search code examples
flutterdartprovider

Prevent re-initialization of Provider in Flutter web


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");

Solution

  • 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 :)