小编Vla*_*ken的帖子

在 Android Jetpack Compose 中使用 State 时出现 java.lang.IllegalStateException

ViewModel使用 Kotlin 密封类为 UI 提供不同的状态。此外,我使用androidx.compose.runtime.State对象来通知 UI 状态的变化。

如果错误MyApi发生的要求,我把UIState.FailureMutableState对象,然后我得到IllegalStateException

 java.lang.IllegalStateException: Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied
        at androidx.compose.runtime.snapshots.SnapshotKt.readError(Snapshot.kt:1524)
        at androidx.compose.runtime.snapshots.SnapshotKt.current(Snapshot.kt:1764)
        at androidx.compose.runtime.SnapshotMutableStateImpl.setValue(SnapshotState.kt:797)
        at com.vladuken.compose.ui.category.CategoryListViewModel$1.invokeSuspend(CategoryListViewModel.kt:39)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Run Code Online (Sandbox Code Playgroud)

ViemModel 代码:

@HiltViewModel
class CategoryListViewModel @Inject constructor(
    private val api: MyApi
) : ViewModel() {

    sealed class UIState …
Run Code Online (Sandbox Code Playgroud)

android kotlin kotlin-coroutines android-jetpack-compose

6
推荐指数
3
解决办法
998
查看次数