Advertisement

TrimwebsolutionsTrimwebsolutions

Problem and Solutions

How can I assign a private mutableStateOf to a State variable Jetpack in Android ?

By M.K. Verma · 4 May 2022

How can I assign a private mutableStateOf to a State variable Jetpack in Android ?
// _isRecording is a Boolean
private var _isRecording by mutableStateOf(false)
// _isRecording is a State<Boolean>
private var _isRecording = mutableStateOf(false)

This is why you're getting this error.

You should expose like this:

class SoundViewModel @Inject constructor(): ViewModel() {

    private var _isRecording = mutableStateOf(false)
    val isRecording: State<Boolean> = _isRecording
}

and consume like this:

@Composable
fun YourComposable(soundViewModel: SoundViewModel) {
    val isRecording by soundViewModel.isRecording