Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StateFlow.value should not be called within composition

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?

like image 342
jc12 Avatar asked Sep 03 '25 02:09

jc12


2 Answers

In my case, I added .collectAsState() like this

@Composable
fun yourScreen() {
    val state = viewModel.viewState.collectAsState()
    when (state.value) {
        // your code
    } 
}
   
like image 58
Cabezas Avatar answered Sep 08 '25 00:09

Cabezas


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

like image 21
Jakoss Avatar answered Sep 08 '25 02:09

Jakoss