<@UJBPFB3SN> what is the difference between these ...
# compose
b
@Ian Lake what is the difference between these two approaches
Copy code
data class Person(val name: String)
class PersonViewModel: ViewModel() {
    private val _itemsFlow: MutableStateFlow<List<Person>> = MutableStateFlow(emptyList())
    val itemsFlow: StateFlow<List<Person>> = _itemsFlow

    private val _itemsState: MutableState<List<Person>> = mutableStateOf(emptyList())
    val itemsState: State<List<Person>> = _itemsState
}

@Composable
fun PersonScreen(viewmodel: PersonViewModel = viewModel()) {
    val itemsFromState: List<Person> by viewmodel.itemsState
    val itemsFromFlow: List<Person> by viewmodel.itemsFlow.collectAsState()
}
they are all returning
List<Person>
and recomposed whenever data changes
1
i
You don't have to specifically mention anyone, there's plenty of people who can answer questions in the channel (and generally, you should try to avoid pasting more than a few lines of code in your original post - try to add code as a reply to your question so that it doesn't overwhelm the channel in general)
👍🏼 2
👍 4
But to answer your question, they'll both give you ~the same behavior in your UI layer
Obviously if you are getting your data from a database repository, those usually provide a Flow, which usually means you should just stay with a Flow up through each layer
x
MutableState<T>
is pretty limited in terms of doing further asynchronous operations like
.map
,
.flatMap
.reduce
, and you cant easily cross the thread boundaries with different dispatchers
🙏 1
c
MutableState
has never been described as a reactive stream. If you need that behavior then
Flow
is your best option. However, you can quite easily perform suspending operations like that in a
LaunchedEffect
.
1
b
i think the best option would be to expose a stateflow and collect it as state