Hello, can someone help me understand why I'm gett...
# compose
s
Hello, can someone help me understand why I'm getting
java.lang.IllegalStateException: Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied
for this composable when doing a state read in the layout scope. The problem doesn't occur when I change using
graphicsLayer
from the lambda style (layout layer update) to the parameters arguments one (composition layer)
(too big to paste here)
Also the crash seems to be caused by some kind of race condition as it some times doesn't crash (I have UI unit test that show the erratic behaviour)
If I change all the
graphicsLayer
modifiers no more crash 😕
Here is an example from the official docs doing the same https://developer.android.com/jetpack/compose/phases#phase2-layout
s
Reading something from inside the lambda should be fine, since as you say the official docs show doing it too. The code you pasted is quite long, mind sharing where exactly it crashes from the 300+ line of that snippet?
s
Example: L237
Copy code
.graphicsLayer {
                    translationX = (if (rightSide) displacement.dp else -displacement.dp).toPx()
                    translationY = constraints.maxHeight.toFloat() * normalizedOffsetPosition
                }
but if I use parameters it doesn't crash
Copy code
.graphicsLayer(
    translationY = constraints.maxHeight.toFloat() * normalizedOffsetPosition,
    translationX = with(LocalDensity.current) { (if (rightSide) displacement.dp else -displacement.dp).toPx() }
)
The crash stacktrace doesn't direct to these lines in specific as it seems it happens on the composing looper internally Here is the stack stacktrace when running an unit test testing the composable:
The line
Caused by: java.lang.IllegalStateException: Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied
implies I'm reading state before the snapshot was applied or before created, the only place that might happen is in the a coroutine call inside the local function
setScrollOffset
that is called from a listener (global snapshot, but that should still work)
In the end decided to just use the non-lambda parameters
z
Please file a bug