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

AndroidAndroid Jetpack-Compose

Android Problem Overview


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) }

Android Solutions


Solution 1 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElyeView Question on Stackoverflow
Solution 1 - AndroidElyeView Answer on Stackoverflow