https://kotlinlang.org logo
Title
d

dazza5000

05/05/2022, 7:54 PM
is it more idiomatic/preferred to expose a
mutableStateOf
or a
StateFlow
/
LiveData
property from a ViewModel?
l

Landry Norris

05/05/2022, 7:59 PM
I would prefer Flow, since it’s available on all platforms and doesn’t rely on any libraries that aren’t in the standard library.
:kotlin-intensifies: 3
c

Colton Idle

05/05/2022, 8:17 PM
I think mutableStateOf is idiomatic. Definitely not livedata if you can avoid it
2
d

dazza5000

05/05/2022, 8:17 PM
I noticed this https://developer.android.com/jetpack/compose/state#viewmodels-source-of-truth, but it feels like we are putting view implementation/related API's inside the ViewModel if we use
mutableStateOf
l

Landry Norris

05/05/2022, 8:29 PM
If you use Flow, you can create your state in compose using
val state by fooFlow.collectAsState(defaultFoo)
to avoid having to put
state.value
everywhere. You can just use the state variable directly, which I find idiomatic enough. Bonus points for using a data class for state and having your state for the screen all come from a single flow.
👍 1
d

dazza5000

05/13/2022, 3:17 PM
Most of the implementation here exposes flows from the viewmodel and not a
mutableStateOf
property https://github.com/android/nowinandroid