Ionic 4 storage values going null on android device

       this.storage.set('role',"Resellers")

     submit(email, name ){
        
        
        let mail = email
        let nm = name
    
        this.storage.set('name',nm)
    }

the role is set with string and name is set with values passed with submit() function which is called on the button click

I am getting both values on browser but not on device

You are using this Plugin right?

Then your code seems to be right, and as you said it is working on the browser.
So in order to bugfix this we would need more code. What are you doing with the variables after you got them.
Maybe your Code is going forward to fast while android is still loading the storage item?
This happened to me once. Because it takes a while to get the Item from the Device Storage you sometimes must wait for the item and then go on with your code.

Yes I am using same plugin

constructor(private storage: Storage){

this.storage.get('name').then((val) =>{
      console.log("name is", val)
      this.name = val;
      console.log(this.name)

    })

      this.storage.get('role').then((val) =>{
        console.log("role" , val)
        this.role = val;
        console.log(this.role)
  
      })

}

I want to navigate user depending upon values stored, but for the time being I have only this much code in my ts file

Can you please run this on your device and see if values are here:

this.storage.get('name').then((val) =>{
      alert(val);
      this.name = val;
    })

      this.storage.get('role').then((val) =>{
        alert(val);
        this.role = val;
      })

By the way which device are you using?

Okay I will try. I am using realme 3 pro andorid 9

Does this work and display a string? If so, then you have a an async issue where you need to wait for the setter to finish before to get

this.storage.set('stuff','My stuff')
       .then(()=> this.storage.get('stuff'))
       .then((stuff)=>console.log(stuff));

$5 on "forgot to wait on Storage.ready()".

2 Likes

Sorry for the late reply. You are right, this stuff is working properly but now how do I wait?

 this.storage.ready().then(() =>{
      console.log('before')
     return this.storage.get('name')
    
    }).then((name) => console.log('after' + name) )

I have tried something like this which is also returning null

async getName() {
    await this.storage.ready();
    console.log('ready');
    const name = await this.storage.get('name');
    console.log('retrieved', name);
}
1 Like

No . Still returning null on android

How are you setting name in storage? Perhaps it’s not being set properly (or storage.ready() again) if you are still getting null

yes. I have set properly. I am getting proper values with ionic serve

The code you posted for the submit method does not include storage.ready() before storage.set()

The problem is I am setting values on another page and getting values on another. Is that the reason for await issue?

And this is why I keep insisting that people should not use storage for in-app communication. If you only use storage to communicate between this app run and the next, you never have to worry about any of this. Use a mutually injected service provider instead to communicate amongst different parts of the app.

But I want to redirect user depending upon values set. I value is set which means old user redirect to some page , if value is not set means new user redirect to details page. How can I achive this ?

Read from storage only once, at application startup, and then perform your routing accordingly.

Yes, But it is going null on android . What should I do now?

Apologies if English is not your first language, but to say something is “going null” implies the following sequence of events to me:

  • X is not null at some point in time
  • your app code provably does nothing to change the value of X
  • at some later date, X becomes null

There have been many suggestions in this thread, and I am now lost as to what your code is, what problems have been solved, and what problems remain.

Please try reading this thread and see if it seems relevant to your situation.