lesnitsky / flutter_localstorage

📦 LocalStorage for Flutter
MIT License
298 stars 65 forks source link

localstorage returns null on getItem #48

Closed ksydex closed 4 years ago

ksydex commented 4 years ago

I want to init store with value from localstorage, but it always return null there. Then I tried to getItem from localstorage in widget with this code: print('from local storage: '+localStorage.getItem('user').toString()); And i get next output:

Performing hot restart...
Syncing files to device sdk gphone x86 arm...
Restarted application in 936ms.
I/flutter ( 4982): from local storage: null
I/flutter ( 4982): from local storage: {socialAccounts: [], culture: {key: en}, currency: {key: RUB}, id: 6, email: ..... } 

So, it is being called two times, where first returns null and second - actual value. What's the problem?

lesnitsky commented 4 years ago

make sure to await storage.ready before calling getItem

ksydex commented 4 years ago

@lesnitsky Yes, this worked. Would be good to mention what await storage.ready is actually doing to prevent issues like this :) Thank you!