gpt4 book ai didi

android - Jetpack Compose – 我需要使用哪个依赖项才能使用状态委托(delegate)?

转载 作者:行者123 更新时间:2023-12-04 13:09:57 26 4
gpt4 key购买 nike

代码实验室中提供的示例存在编译时错误。

这是 View 模型

class VM : ViewModel() {
val isVisible = MutableStateFlow(true).asStateFlow()
}

这就是我想写的:

@Composable
fun Whatever(vm: VM = viewModel()) {
val isVisible by vm.isVisible.collectAsState(true)
// Use is visible here
}

但它会产生以下错误:

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

这真的很奇怪,因为我可以这样使用它:

@Composable
fun Whatever(vm: VM = viewModel()) {
val isVisible = vm.isVisible.collectAsState(true)
if(isVisible.value) { … }
}

我需要引入什么依赖项才能使委托(delegate)工作?

这是我的依赖项:和 compose_version = 1.0.0-beta01

dependencies {
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'

implementation "androidx.activity:activity-compose:1.3.0-alpha03"
implementation "androidx.compose.runtime:runtime:$compose_version"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.foundation:foundation-layout:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.material:material-icons-extended:$compose_version"
implementation "androidx.compose.foundation:foundation:$compose_version"
implementation "androidx.compose.animation:animation:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
implementation "androidx.compose.runtime:runtime:$compose_version"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"

implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'

// Kotlin coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

implementation 'androidx.appcompat:appcompat:1.3.0-beta01'
implementation 'androidx.activity:activity-ktx:1.2.0'
implementation 'androidx.core:core-ktx:1.5.0-beta02'
implementation "androidx.activity:activity-compose:1.3.0-alpha03"

implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha02"

androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation "androidx.compose.ui:ui-test:$compose_version"
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
}

最佳答案

doc 中所述,尝试添加以下导入:

import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState

关于android - Jetpack Compose – 我需要使用哪个依赖项才能使用状态委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66497140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com