I downloaded Android Studio Bumblebee beta today and noticed a new warning:
"StateFlow.value should not be called within composition"
Why shouldn't we call StateFlow.value within composition?
In my case, I added .collectAsState()
like this
@Composable
fun yourScreen() {
val state = viewModel.viewState.collectAsState()
when (state.value) {
// your code
}
}
Because in composition you should be listening to events, not just reading the current one. If you read only the current one then your view won't get recomposed after the value is changed.
I mean - maybe there is a valid use case for that, but i never encountered one. I guess the warning you saw is just to warn users that they are trying to do something they probably don't want to
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With