'What are differents between handleMeter.uiState.collectAsState() and remember { handleMeter.uiState } in Kotlin?

uiState is MutableStateFlow in ViewModel() , Code A and Code B can update UI automatically based latest value of the flow.

1: What are differents between handleMeter.uiState.collectAsState() and remember { handleMeter.uiState } in Kotlin ?

2:Code C is wrong, I can't wrapped handleMeter.uiState.collectAsState() with remember , why? How can I remember the latest value of the flow?

Code A

@Composable
fun Greeting(handleMeter: HandleMeter,lifecycleScope: LifecycleCoroutineScope) {
    Column(
        modifier = Modifier.fillMaxSize()
       
    ) {   
        var  dataInfo = handleMeter.uiState.collectAsState()
        Text(text = "My ${dataInfo.value}")
   }
   ..
}

class HandleMeter: ViewModel() {
    val uiState = MutableStateFlow<Int>(10)
    ...
}

Code B

@Composable
fun Greeting(handleMeter: HandleMeter,lifecycleScope: LifecycleCoroutineScope) {
    Column(
        modifier = Modifier.fillMaxSize()
       
    ) {    
        var dataInfo = remember { handleMeter.uiState }
        Text(text = "My ${dataInfo.value}")
   }
   ..
}

//The same

Code C (It's wrong)

@Composable
fun Greeting(handleMeter: HandleMeter,lifecycleScope: LifecycleCoroutineScope) {
    Column(
        modifier = Modifier.fillMaxSize()
       
    ) {   
        var  dataInfo = remember { handleMeter.uiState.collectAsState() }
        Text(text = "My ${dataInfo.value}")
   }
   ..
}

//The same


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source