Is there a way to leverage `remember` in combinat...
# compose
k
Is there a way to leverage
remember
in combination with Proto DataStores? I've defined my
DataStore
:
Copy code
val Context.dataStore: DataStore<AppPreferences> = dataStore(serializer = ..., ...)
However, I would like to have a way to fetch single values inside composables:
Copy code
@Composable
fun SomeUI() {
    val context = LocalContext.current
    var userFontSize by remembe { context.dataStore.data.fontSize } // or something similar

    // Re-composition happens if the value changes for some reason. And it also gets stored if the var changes.
}
It's easy to do with a
SharedPreferences
-backed
DataStore
since I can use keys to fetch the value and set it. But I am not sure how to do it with Protobufs.
a
You should be able to observe the data store's flow with
collectAsState
or
produceState
k
collectAsState
only makes is so I can keep up-to-date with it being changed externally. And I do not see how
produceState
helps me here. Isn't that function meant for data that, without any influence, changes? Like timers. I just don't see how I can use it to update my data store when I assign a new value:
userFontSize = 12
.
s
When you edit your DataStore the flows automatically emit the new value. So if you start collecting that flow with either way that Adam suggests you will receive the latest value when it changes.
☝️ 1