var value by remember { mutableStateOf(default) } 产生错误,为什么?

Ely*_*lye 33 android android-jetpack-compose

我指的是https://developer.android.com/jetpack/compose/state 中的示例。当我编码时

var expanded by remember { mutableStateOf(false) }
Run Code Online (Sandbox Code Playgroud)

它错误说明

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

下面虽然有效

val expanded = remember { mutableStateOf(false) }

// OR

val (expanded, setExpanded) = remember { mutableStateOf(false) }
Run Code Online (Sandbox Code Playgroud)

Ely*_*lye 79

显然,我必须包括这些进口

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
Run Code Online (Sandbox Code Playgroud)

自动导入不会在测试版 Android Studio 4.2 中自动推荐

如果您使用 livedata,请考虑以下导入

import androidx.compose.runtime.livedata.observeAsState
Run Code Online (Sandbox Code Playgroud)


Thr*_*ian 6

它会产生错误,因为 的委托函数MutableState是扩展函数,因为它们需要导入。

/**
 * Permits property delegation of `val`s using `by` for [State].
 *
 * @sample androidx.compose.runtime.samples.DelegatedReadOnlyStateSample
 */
@Suppress("NOTHING_TO_INLINE")
inline operator fun <T> State<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value

/**
 * Permits property delegation of `var`s using `by` for [MutableState].
 *
 * @sample androidx.compose.runtime.samples.DelegatedStateSample
 */
@Suppress("NOTHING_TO_INLINE")
inline operator fun <T> MutableState<T>.setValue(thisObj: Any?, property: KProperty<*>, value: T) {
    this.value = value
}

/**
 * A mutable value holder where reads to the [value] property during the execution of a [Composable]
 * function, the current [RecomposeScope] will be subscribed to changes of that value. When the
 * [value] property is written to and changed, a recomposition of any subscribed [RecomposeScope]s
 * will be scheduled. If [value] is written to with the same value, no recompositions will be
 * scheduled.
 *
 * @see [State]
 * @see [mutableStateOf]
 */
@Stable
interface MutableState<T> : State<T> {
    override var value: T
    operator fun component1(): T
    operator fun component2(): (T) -> Unit
}
Run Code Online (Sandbox Code Playgroud)

import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
Run Code Online (Sandbox Code Playgroud)

getValuesetValue导入函数。

使用 Kotlin 委托,您可以创建委托函数作为类的一部分

/**
 * Delegate [MyState] to this function
 */
fun myStateOf() = MyState()

class MyState internal constructor() {
    var value = 0

    operator fun getValue(thisRef: Any?, property: KProperty<*>): Int = value

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Int) {
        this.value = value
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用它们而无需导入,因为它们是类的一部分

var myState by myStateOf()
myState = 5
Run Code Online (Sandbox Code Playgroud)

或者将它们定义为扩展函数,如StateMutableState接口的情况。

/**
 * Delegate [MyState] to this function
 */
fun myStateOf() = MyState()

class MyState internal constructor() {
    var value = 0
    
}

@Suppress("NOTHING_TO_INLINE")
inline operator fun MyState.getValue(thisRef: Any?, property: KProperty<*>): Int = value


@Suppress("NOTHING_TO_INLINE")
inline operator fun MyState.setValue(thisObj: Any?, property: KProperty<*>, value: Int) {
    this.value = value
}
Run Code Online (Sandbox Code Playgroud)

当你尝试打电话时

var myState by myStateOf()
myState = 5
Run Code Online (Sandbox Code Playgroud)

您收到错误,要求您导入 getValue 和 setValue 函数。

在此输入图像描述