When using a specific name for the DataStore ("settings" in this case), the Preference value is not null the first time I open the app. But if I change the DataStore name to something different, it will work fine. Any clue why this is happening?
Here's the broken code:
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
class SettingsDataStore @Inject constructor(@ApplicationContext private val context: Context) {
private val showPermissionDialogKey = booleanPreferencesKey("isPermissionDialogShown")
val showPermissionDialog: Flow<Boolean> = context.dataStore.data.map { preferences ->
preferences[showPermissionDialogKey] ?: true
}
suspend fun setPermissionDialogShown() {
context.dataStore.edit { preferences ->
preferences[showPermissionDialogKey] = false
}
}
}
And here's the working code:
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "my_settings")
// Same as before ...
Note that I just changed the name of the DataStore from "settings" to "my_settings".