首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jetpack组合-我需要使用哪个依赖项才能使用状态委托?

Jetpack组合-我需要使用哪个依赖项才能使用状态委托?
EN

Stack Overflow用户
提问于 2021-03-05 17:38:56
回答 1查看 3K关注 0票数 3

我在代码实验室提供的样本中有一个编译时错误。

这是ViewModel

代码语言:javascript
复制
class VM : ViewModel() {
     val isVisible = MutableStateFlow(true).asStateFlow()
}

这就是我想写的:

代码语言:javascript
复制
@Composable
fun Whatever(vm: VM = viewModel()) {
    val isVisible by vm.isVisible.collectAsState(true)
    // Use is visible here
}

但是,它会产生以下错误:

键入'State‘没有方法'getValue(Nothing?,KProperty<*>)’,因此它不能充当委托。

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

代码语言:javascript
复制
@Composable
fun Whatever(vm: VM = viewModel()) {
    val isVisible = vm.isVisible.collectAsState(true)
    if(isVisible.value) { … }
}

我需要引入什么样的依赖性才能使委托工作?

下面是我的依赖关系:和compose_version = 1.0.0-beta01

代码语言:javascript
复制
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"
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-05 17:45:16

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

代码语言:javascript
复制
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66497140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档