简体   繁体   English

Jetpack Compose – 我需要使用哪个依赖项才能使用 state 代表?

[英]Jetpack Compose – Which dependency do I need to use to be able to use state delegates?

I have a compile-time error with the samples provided in the code-labs.代码实验室中提供的示例出现编译时错误。

This is the ViewModel这是视图模型

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

This is what I'd like to write:这就是我想写的:

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

But it yields to the following error:但它会产生以下错误:

Type 'State<TypeVariable(R)>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate类型 'State<TypeVariable(R)>' 没有方法 'getValue(Nothing?, KProperty<*>)' 因此它不能用作委托

Which is really strange because I can use it like so:这真的很奇怪,因为我可以这样使用它:

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

What dependency do I need to bring in to make the delegate work?我需要引入什么依赖项才能使委托工作?

Here are my dependencies: and compose_version = 1.0.0-beta01这是我的依赖项:和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"
}

As described in the doc , try to add the following imports:文档中所述,尝试添加以下导入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Jetpack Compose 中我们还需要 LiveData 吗,还是我们可以只使用 Compose State? - Do we still need LiveData in Jetpack Compose, or we can just use Compose State? 为什么我无法在 jetpack compose 中使用带有 firebase 的移动身份验证? - Why I am not able to use mobile authentication with firebase in jetpack compose? 如何直接在 Jetpack Compose 中使用 Color 资源? - How do I use Color resource directly in Jetpack Compose? 除了状态提升之外,我可以与 Jetpack Compose 一起使用的其他状态管理选项? - Other state management options that I may use with Jetpack Compose, beyond State hoisting? 如何将 CameraView 与 Jetpack Compose 结合使用? - How can I use a CameraView with Jetpack Compose? 在 Jetpack Compose 中使用 NavHostFragment - Use NavHostFragment in Jetpack Compose 我是否需要将WeakReference用于循环依赖? - Do I need to use a WeakReference for circular dependency? 我需要使用 swipe 来显示和 swipe 与 jetpack compose android 一起关闭 - I need to use swipe to reveal and swipe to dismiss together with jetpack compose android 当我使用 Jetpack Compose 时,哪个 scope 会影响 LocalContentAlpha.current? - Which scope will LocalContentAlpha.current effect when I use Jetpack Compose? 如何在 Jetpack Compose 中使用小部件? - How to use widgets in Jetpack Compose?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM