I am trying to save and read a boolean in storage to show or hide a disclaimer using ionic-storage.
I have tried various ways but all fail.
Saving:
this.disclaimerOn = true;
this.storage.set('disclaimer', JSON.stringify(this.disclaimerOn));//save true
this.storage.set('disclaimer', this.disclaimerOn));
this.disclaimerOn = false;
this.storage.set('disclaimer', JSON.stringify(this.disclaimerOn));//save false
this.storage.remove('disclaimer');
Retrieving:
this.disclaimerOn = this.storage.get('disclaimer');
var temp: any = this.storage.get('disclaimer');
this.disclaimerOn = JSON.parse(temp);
However, window.localStorage works:
window.localStorage.setItem('disclaimer', 'true');//save true
window.localStorage.removeItem('disclaimer');//remove item
if (window.localStorage.getItem('disclaimer')) {
this.disclaimerOn = window.localStorage.getItem('disclaimer');
}
Ionic-storage is working fine for me otherwise.
1 Like
Ionic-storage is asynchronous.
this.storage.get('disclaimer').then(disc => this.disclaimerOn = disc);
1 Like
Darn, I came to the forums too hastily. Should have opened my eyes further this morning.
Thanks rapropos - for all your forum efforts.
When I run into things like this that just seem impossibly mystical, I try to let tsc
lead me out of the cave.
First up: eliminate any
from your vocabulary. Just don’t let yourself type it. Ever. (OK, there are some rare occasions where it really is needed, but I’d say based on a sample of code I see in forum posts saying one in a thousand is legit is generous).
Next: separate each line of problematic code into its own function. Explicitly type all of its arguments and don’t forget to also type the return value.
At some point in this exercise I almost always run into a situation where tsc
will tell me that I can’t assign a Foo
to a Bar
, indicating that one of my assumptions about the type of something buried in a snarl of then
s and map
s and subscribe
s is wrong, and this process helps me pin down exactly where it is and what I am misunderstanding.
1 Like