com.google.firebase.datastorage

Extension functions summary

T
<T : Any?> Preferences.getOrDefault(key: Preferences.Key<T>, defaultValue: T)

Helper method for getting the value out of a Preferences object if it exists, else falling back to the default value.

Extension functions

getOrDefault

fun <T : Any?> Preferences.getOrDefault(key: Preferences.Key<T>, defaultValue: T): T

Helper method for getting the value out of a Preferences object if it exists, else falling back to the default value.

This is primarily useful when working with an instance of MutablePreferences

Example:

dataStore.editSync((pref) -> {
long heartBeatCount = DataStoreKt.getOrDefault(pref, HEART_BEAT_COUNT_TAG, 0L);
heartBeatCount+=1;
pref.set(HEART_BEAT_COUNT_TAG, heartBeatCount);

return null;
});
Parameters
key: Preferences.Key<T>

The typed key of the entry to get data for.

defaultValue: T

A value to default to, if the key isn't found.