var value by remember { mutableStateOf(default) } produce error, why?

I’m referring to the example in https://developer.android.com/jetpack/compose/state. When I code

var expanded by remember { mutableStateOf(false) }

It errors stating

Type 'TypeVariable(T)' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate

The below works though

val expanded = remember { mutableStateOf(false) }

// OR

val (expanded, setExpanded) = remember { mutableStateOf(false) }

Answer

Apparently, I have to include these imports

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue

The auto imports don’t automatically recommend it in the beta Android Studio 4.2

If you use livedata, then consider the below import

import androidx.compose.runtime.livedata.observeAsState

Leave a Reply

Your email address will not be published. Required fields are marked *