Thread
#compose
    Ashu

    Ashu

    11 months ago
    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?
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    11 months ago
    What's the error?
    m

    mkrussel

    11 months ago
    Ashu

    Ashu

    11 months ago
    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

    11 months ago
    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
    .
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    10 months ago
    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:
    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.