https://kotlinlang.org logo
#compose
Title
# compose
a

Ashu

10/26/2021, 6:40 PM
Coroutine launch is showing error when launched from a composable function. I am trying to show/hide ModalBottomSheetLayout by observing a state flow. How can I resolve this. Also, how do I implement multiple bottom sheets in same Layout?
z

Zach Klippenstein (he/him) [MOD]

10/26/2021, 7:23 PM
What's the error?
m

mkrussel

10/26/2021, 7:26 PM
a

Ashu

10/27/2021, 8:23 AM
Copy code
val hideBottomSheet by hideBottomSheetFlow.collectAsState()

coroutineScope.launch {
    if (hideBottomSheet) bsScaffoldState.hide()
    else bsScaffoldState.show()
}
Here launch is showing "warning error" that
Calls to launch should happen inside a LaunchedEffect and not composition
m

mkrussel

10/27/2021, 12:30 PM
You don't need to launch to access data from a state. Just remove the launch.
collectAsState
does a launch for you inside a
LaunchedEffect
.
z

Zach Klippenstein (he/him) [MOD]

10/28/2021, 4:57 PM
The ideal approach here is to hoist the scaffold state so that whatever is emitting on
hideBottomSheetFlow
can just call the show/hide methods itself. If you need to wire up events from a composable like this, you’d want to do something like this:
Copy code
LaunchedEffect(hideBottomSheetFlow) {
  hideBottomSheetFlow.collect { hideBottomSheet ->
    if (hideBottomSheet) bsScaffoldState.hide() else bsScaffoldState.show()
  }
}
You don’t need
collectAsState
here because that flow doesn’t represent state, it’s events.
8 Views