kotlinandroid-studio

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


@Composable fun MyComposeUI() {
    var sliderPosition by remember { mutableStateOf(0.5f) }
}

the error is on the line:

var sliderPosition by remember { mutableStateOf(0.5f) }

should ?I choose the suggested fix: import operator 'State.getValue' ?

this is a screenshot showing the error:

Error message


Solution

  • Place the text cursor over the error message and press Alt+Enter to automatically create the missing import statement:

    import androidx.compose.runtime.getValue
    

    Since this is a var you need to repeat the above to also add this:

    import androidx.compose.runtime.setValue
    

    You can even enable the "Add unambiguous imports on the fly" option for Kotlin in the Android Studio settings under Editor > General > Auto Import so the imports are added automatically when you tpye, you do not even need to press the above shortcut anymore.