Reputation: 1829
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?
Upvotes: 33
Views: 11588
Reputation: 10717
In my case, I added .collectAsState()
like this
@Composable
fun yourScreen() {
val state = viewModel.viewState.collectAsState()
when (state.value) {
// your code
}
}
Upvotes: 44
Reputation: 5235
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
Upvotes: 23