android - 可组合调用只能在可组合函数的上下文中发生
问题描述
我有查询需要致电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()
}
})
}
现在我想传递b
给Composble
:
@Composable
private fun Content(b: Boolean) {
}
我应该怎么称呼 Content since @Composable invocations can only happen from the context of a @Composable function
?如果我将内容放在启动块中,我会收到上述错误消息。
解决方案
您可以使用作用域进行后台调用,例如从数据库中获取数据,并利用可变状态将其传递给可组合对象。
例如:
val foo = remember { mutableStateOf("") }
LaunchedEffect(true) {
foo.value = bar()
}
Text (text = foo.value)
推荐阅读
- c++ - 其他类的成员函数对成员函数的嵌套引用
- c# - 如何在 ASP.Net Core 2.0 Razor 页面中发布 IFormFile?
- r - r 更改列名列表(如果存在)
- python - Pandas Max Min 之间的索引范围
- mamp - 在 OS X Apache 上为 python2.7 编译的 wsgi_mod.so 未启动
- vba - 是否可以创建一个返回对象的函数?
- sparql - 将 graphdb SPARQL 浏览器与任何端点一起使用
- javascript - 内容安全策略阻止从 CodePen 到 Github 的链接
- sql - 在 Excel 中使用 SQL 组合来自不同位置的文件
- mysql - 删除前几天的记录,但如果全部删除,保留最后 20 条记录