Getting below error when accessing `layoutCoordina...
# compose
t
Getting below error when accessing
layoutCoordinates.positionInRoot()
. This is only happening in release build.
Copy code
Fatal Exception: java.lang.IllegalStateException
LayoutCoordinate operations are only valid when isAttached is true
compose version =
1.1.0-rc01
I can wrap it inside a
if(layoutCoordinates.isAttached)
but am thinking why this is happening only on release build? 🤔
d
Do you have a snippet to repro this?
t
I haven’t tried this in isolation 🤔 Will try and post the result here 👍
but any idea why it happened only in release build? 🤔
I have seen similar issue closed in one of the prev releases -> https://issuetracker.google.com/issues/182551468
d
Only in release build? That seems odd. Do you have a stack trace?
t
Copy code
androidx.compose.ui.node.LayoutNodeWrapper.localToRoot-MK-Hz9U (LayoutNodeWrapper.kt:767)
androidx.compose.ui.layout.LayoutCoordinatesKt.positionInRoot (LayoutCoordinatesKt.java:103)
com.my.company.TrackModifierKt$track$1$2$1.invokeSuspend (TrackModifier.kt:104) 
kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:33)
kotlinx.coroutines.DispatchedTask.run (DispatchedTask.kt:106)
androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch (AndroidUiDispatcher.android.kt:81)
androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch (AndroidUiDispatcher.android.kt:41)
androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run (AndroidUiDispatcher.android.kt:57)
android.os.Handler.handleCallback (Handler.java:938)
android.os.Handler.dispatchMessage (Handler.java:99)
android.os.Looper.loop (Looper.java:246)
android.app.ActivityThread.main (ActivityThread.java:8633)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
So
TrackModifier.kt:104
has a
positionInRoot()
call inside a
onGloballyPositioned
modifier
d
Are you doing the query in the
onGloballyPositioned
callback, or did you cache the layoutCoordinates and used it at a different time?
t
Ahh.. now am getting it. There’s coroutine involved here. The layoutCoordinates are used just after the launch. During the scroll the slight delay might be causing the issue. but am wondering why it wasn’t happening before 🤔
Anyways, that should be the reason.
d
Yea, the
positionInRoot
only guarantees correctness if you query it after the layout pass is done.
t
what if i use the
isAttached
flag before accessing
positionInRoot
? would that help me with the crash. ?
d
That'll probably work. By the time the node you are querying is attached, all its ancestors would've been attached also.
t
Cool 👍 Will give it a try.
349 Views