I want to call a suspend-function inside of a callback of composable-function.
suspend fun getLocation(): Location? { /* ... */ }
@Composable
fun F() {
val (location, setLocation) = remember { mutableStateOf<Location?>(null) }
val getLocationOnClick: () -> Unit = {
/* setLocation __MAGIC__ getLocation */
}
Button(onClick = getLocationOnClick) {
Text("detectLocation")
}
}
If I would have used Rx then I could just subscribe.
I could do invokeOnCompletion and then getCompleted, but that API is experimental.
I can't use launchInComposition in getLocationOnClick because launchInComposition is @Composable and getLocationOnClick can not be @Composable.
What would be the best way to get result of a suspending function inside a regular function, inside @Composable function?
Create a coroutines scope, tied to the lifecycle of your composable, and use that scope to call your suspending function