Ionic Storage not working as expected

i am fetching the stored data in ionic storage but i am getting null any help???

Post some code, please, then we can help you.

I’m just storing username and trying to fetch that in same function. It is returning null. username type is of ‘String’.

storage.get and storage.set are asynchronous funcions. Problably storage.get has been executed before storage.set finish. Try it to confirm:

this.storage.set('username', this.username).then(() => {
  this.storage.get('username').then(data => {
    console.log(data)
  })
})
1 Like

While @jnascimento is absolutely correct in their analysis, I would urge you not to do this as a matter of principle, because it means you have to worry religiously about race conditions in all your code that interacts with storage.

Instead, use storage only to communicate with future invocations of the app, writing optimistically and reading only once, at app startup. For all in-app communication, eschew storage in favor of data held in a service provider and shared (preferably exposed as Observables).

2 Likes

thank you
it is working