问题
I am using localstorage to store some basic data in my flutter web app. Everything works fine but my problem is that the data is not available when I restart the browser.
// setting the data
html.window.localStorage["user-key"] = "Hello There";
//Reading it
String val = html.window.localStorage["user-key"];
I want to know if this is the intended behavior or intended behavior for testing in flutter web or an error on my side.
回答1:
According to the documentation it is supposed to stay across different sessions including closing and re-opening the browser. Check here.
I suspect you might be overwriting the key. So the idea would be check if the storage already has the key and then populate it as suggested in the documentation.
Also in flutter web debug mode you are opening a new profile of chrome instance if I am right, which doesn't have access to other instance storage. Correct me if I am wrong.
You can test this by accessing the URL in your usual browser instance instead of the one opened by flutter and closing it and reopening it again.
There are several other posts in SO which talk about this behavior of local storage and possible causes.
来源:https://stackoverflow.com/questions/60848470/flutter-web-local-storage