what does this error means? ```Reading a state tha...
# compose
m
what does this error means?
Copy code
Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied
c
Paste the full stack trace? This kind of stuff can happen if you’re using mismatched library versions.
m
@cb I made clone of
JetNews
for practise. This happens when I click on
Home
drawer item when
Home
is already selected
Copy code
E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
    Process: com.marko.myjetnews, PID: 6766
    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:1521)
        at androidx.compose.runtime.snapshots.SnapshotKt.current(Snapshot.kt:1697)
        at androidx.compose.runtime.SnapshotMutableState.setValue(MutableState.kt:326)
        at com.marko.myjetnews.home.HomeViewModel.setPosts(HomeViewModel.kt:39)
        at com.marko.myjetnews.home.HomeViewModel.access$setPosts(Unknown Source:0)
        at com.marko.myjetnews.home.HomeViewModel$1.invokeSuspend(HomeViewModel.kt:33)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        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)
in this code there is link to where error occurs
Copy code
var posts by savedStateHandle.mutableStateOf<List<Post>>(
        key = SAVED_POSTS,
        initialState = emptyList(),
        save = { bundleOf(SAVED_POSTS to it) },
        restore = { it.getParcelableArrayList<Post>(SAVED_POSTS) ?: emptyList() },
    )
        private set
Copy code
fun <T> SavedStateHandle.mutableStateOf(
    key: String,
    initialState: T,
    save: (T) -> Bundle,
    restore: (Bundle) -> T,
): MutableState<T> {
    val bundle: Bundle? = get(key)
    val initial = if (bundle == null) initialState else restore(bundle)
    val state = mutableStateOf(initial)
    setSavedStateProvider(key) { save(state.value) }
    return state
}
c
This error indicates that an instance created by
mutableStateOf
was read before snapshot it was created in was applied. This is most-likely caused by creating a
muttableStateOf
in composition and capturing it in a lambda invoked outside composition (such as in a coroutine). My guess, from the stacks, is a coroutine that (probably indirectly) captured the
posts
field of
HomeViewModel
was started before the composition that create the view model was applied. Consider using
LaunchEffect
to start the coroutine instead as it waits until the composition applies successfully to start the coroutine avoiding such issues. Alternately, you can create the view model outside composition and pass it in as a parameter. All
mutableStateOf
created outside a snapshot (such as taken during composition) are automatically applied before any other snapshot is taken.
m
Thanks @Chuck Jazdzewski [G]
1913 Views