I've got this code: ``` var connectionFlow: Mut...
# compose
r
I've got this code:
Copy code
var connectionFlow: MutableStateFlow<GattConnectionState> = MutableStateFlow(GattConnectionState.STATE_DISCONNECTED)
    val bluetoothConnectionState by connectionFlow.collectAsState()
    val isConnected = bluetoothConnectionState == GattConnectionState.STATE_CONNECTED

    LaunchedEffect(address) {
        connectionFlow = viewModel.connect(address)
    }
I think it's not recomposing when I replace
connectionFlow
, what's the correct way to implement this?
s
You are creating a new
connectionFlow
on each recomposition with the code you provide here at least. Is that what you want here? Also, it’s not inside a MutableState, so if you change a var like that compose has no reason to recompose at all
Copy code
@Composable
fun asd() {
  var x = 1
  ...
  LaunchedEffect(Unit) { x = x + 1 }
}
This will of course not trigger a recomposition as a normal var which is not backed by MutableState won’t inform composition that there’s something that has changed and a recomposition needs to happen.
r
Hmm, I pushed the flow creation onto the ViewModel. Good to know on the recreation.