Home > database >  Composable invocations can only happen from the context of a Composable function
Composable invocations can only happen from the context of a Composable function

Time:10-18

I have query that I need to call in scope.launch:

   composeView {
            MyTheme {
                // Create a CoroutineScope that follows this composable's lifecycle
                val composableScope = rememberCoroutineScope()
                LaunchedEffect(key1 = "", block = {
                    composableScope.launch {
                        val b: Boolean = database.query()
                    }
                })
            }

Now I want to pass b to a Composble :

@Composable
    private fun Content(b: Boolean) {

    }

How should I call Content since @Composable invocations can only happen from the context of a @Composable function? If I put Content inside launch block, I receive above error message.

CodePudding user response:

You can use the scopes to make the background calls like fetching from the database and make use of mutable states to pass it on to a composable.

For Example:

val foo = remember { mutableStateOf("") } 

LaunchedEffect(true) { 
  foo.value = bar()
}

Text (text = foo.value)
  • Related