about `#AndroidDevChallenge` … My github actions f...
# compose
s
about
#AndroidDevChallenge
… My github actions fails on this test:
Copy code
com.example.androiddevchallenge.ExampleInstrumentedTest > sampleTest[test(AVD) - 10] FAILED 
254
	kotlin.UninitializedPropertyAccessException: lateinit property remeasurement has not been initialized
255
	at androidx.compose.foundation.lazy.LazyListState.snapToItemIndexInternal$foundation_release(LazyListState.kt:186)
I haven’t touch that part. Any of you faced similar?
🚫 1
a
snapToItemIndex
seems to be called before measuring finished. from where do you call it?
s
roughly this is it:
Copy code
Box() {
        val scrollState = rememberLazyListState()
        LazyRow(state = scrollState) {
            items(1000) {
                Text(text = text(it))
            }
        }

        scope.launch {
            scrollState.scrollToItem(index, scroll)
        }
    }
a
running coroutine right from the composable is never what you want. composable can be recomposed multiple times and then your task will be run again on every recomposition
you basically want to set an initial scroll position, right? the correct way to do it is by passing them as a params to 
rememberLazyListState()
function
s
So how we can achieve the same behaviour where we have to do some suspend task with the preferred way?
b
Yes, that's what I have in mind. I don't know how the rule would be enforced, but if the answer is never use launch, maybe there is a thing to automate.
s
@Andrey Kulikov what I want is, when item\scroll changes scroll the view accordingly. not just initially. item\scroll is calculated through a mutable state.
a
@Shivam Sethi Use
LaunchedEffect
.
s
also used that, did not saw a noticable effect on my use.
a
Copy code
val scrollState = key(index, scroll) { rememberLazyListState(index, scroll) }
        LazyRow(state = scrollState)
maybe something like this will work for you?
s
hey, this is actually great. and this is how I collected scroll changes as well:
Copy code
LaunchedEffect(scrollState) {
            snapshotFlow { scrollState.firstVisibleItemIndex + (scrollState.firstVisibleItemScrollOffset / cellWidthPx).roundToInt() }
                .distinctUntilChanged()
                .filterNot { counting }
                .onEach {
                    onScrolled(it)
                }
                .collect()
        }
👀 1
amazing… thanks 👍🏻