Pick the fix
This doesn't compile. What's the right fix?
@Composable
fun BackToTop(listState: LazyListState) {
Button(onClick = { listState.animateScrollToItem(0) }) {
Text("Top")
}
}Hint 1 of 1
Is onClick a composable or a coroutine?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Get val scope = rememberCoroutineScope() and call scope.launch { listState.animateScrollToItem(0) }
Correct. Event handlers launch into a remembered scope.
Why not the others?
- B Put a
LaunchedEffectinside onClick. LaunchedEffect is a composable; onClick isn't a composable context. - C Use
GlobalScope.launch. It compiles but outlives the screen and touches the list state after it's gone. - D Mark onClick
suspend. The Button's onClick type is a plain () -> Unit.