Android Jetpack Datastore get one stored vďlue

37 views Asked by At

I have some experiance wiriting Android apps. So far I have used SharedPreferences and DefaultSharedPrefrences to store some data like username or name of an account. With shared prefereces i could write something like:

// prefs = Shared Preferences Object
var accountName = prefs.getString("account_name","")

and be quite sure to get a stored string. With datastore I have tried a to follow instructions, descriptions and tutorials found online. My big problem is that they usually have a focus on storing data and more seldom show how data is retrieved. The usually set up a flow from the datastore and never shows how the data is used.

I have tried to use first() on the flow but that is more or less like a crapshot, you very often get a null value instead of the stored data.

My question is:

How can I retrieve a stored value from the datastore and be sure that it is the value from the datastore I get?

For instance I try to get a stored accoiunt name at app startup but usually get a null value.

I have tried something like

prefrencesFlow.first().accountName

and expects a non null string

0

There are 0 answers