JavaDataStorageKt

public final class JavaDataStorageKt


Summary

Public methods

static final @NonNull T
<T extends Object> JavaDataStorageKt.getOrDefault(
    @NonNull Preferences receiver,
    @NonNull Preferences.Key<@NonNull T> key,
    @NonNull T defaultValue
)

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

Public methods

JavaDataStorageKt.getOrDefault

public static final @NonNull T <T extends Object> JavaDataStorageKt.getOrDefault(
    @NonNull Preferences receiver,
    @NonNull Preferences.Key<@NonNull T> key,
    @NonNull T defaultValue
)

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
@NonNull Preferences.Key<@NonNull T> key

The typed key of the entry to get data for.

@NonNull T defaultValue

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