Reading settingsfrom storage


#1

Hello, Im trying to read my settings from ionic/storage so i created a service that will load all the settings if available or load defaults and i did it like this

private theme: BehaviorSubject;

constructor(private storage: Storage) {
this.theme = new BehaviorSubject(‘secondary’); //here im setting the default
this.storage.get(‘theme’).then(data=>{ //here im trying to read it from the storage if its there
if(data) this.theme.next(data);
});
}

and then i subscribe to this on my page to listen to theme change
getTheme(){
return this.theme.asObservable();
}

my problem is the app load the default theme which is the secondary first and then change it to the saved theme
and i dont want that im sure there is something wrong here can someone tell me how to do it better than this?
i tried doing
this.storage.get(‘theme’).then(data=>{
if(data) this.theme.next(data);
else this.theme = new BehaviorSubject(‘secondary’);
});

but then i get can’t read asObservable of undefined

regards


#2

It’s sort of an unavoidable problem. You could display a loading controller during the time the theme is being retrieved from storage, if that would help.