Closed talbiislam96 closed 2 years ago
In my case, I've created a singleton local storage service, declared main function async and await for storage.ready then run the app. I have no issues of data not persisting on hot reload/restarting the app and also in this scenario I don't need future builders everywhere I need to get an item from the storage. Anyway in your implementation, inside of future builders, you should check the connection state->error->data rather than just the data of the snapshot.
@talbiislam96 Try adding await Future.delayed(Duration(seconds:4)); as the first line in your builder, and make sure it is async.
(This is to help bubble up an issue that I suspect the .ready future has.)
In my case, I've created a singleton local storage service, declared main function async and await for storage.ready then run the app. I have no issues of data not persisting on hot reload/restarting the app and also in this scenario I don't need future builders everywhere I need to get an item from the storage. Anyway in your implementation, inside of future builders, you should check the connection state->error->data rather than just the data of the snapshot.
Would you please post your singleton localstorage access class? Thanks!
There is an issue with the map used for caching. I've spent several hours with the issue myself and the only way to solve it, was to implement it from scratch. I've also tried out everything which was suggested by the app developer, but nothing worked. Long story short, in case you are opening and closing the app, you can forget this library
the trick for me was to await storage.ready() after initialisation. So have
final LocalStorage storage = new LocalStorage('blah.json');
await storage.ready;
After that, writing and readying was working fine. I would do a quick check outside of future builders etc, that this works as expected for you.
In your example above, you're not actually checking the value of the future's response, maybe you should show the spinner until it returns true? e.g. something like below?
if (snapshot.data != null && snapshot.data == true) { // local storage is definitely ready now }
For future readers:
make sure to
await storage.ready;
before ANY calls to storage.getItem
setItem
await storage.setItem('foo', 'bar');
^^^^ THIS IS IMPROTANT
I am using the latest version of the package , everything works perfectly until I close the app or use hot reload, the data vanishes .I've tried several solutions (calling storage.ready in a future builder) etc But still no persistent data , this my code , inspired from the example provided with the package
How can I fix the issue for I've been stuck for a while now . Thank you so much for considering helping me