【问题标题】:Title: Type inference failed. Expected type mismatch: inferred type is @Composable, why can't I remember mutableStateOf Composable Function directly?标题:类型推断失败。预期类型不匹配:推断类型为@Composable,为什么我不能直接记住mutableStateOf Composable Function?
【发布时间】:2022-11-04 16:10:29
【问题描述】:

我可以有

val composeFunction = remember { mutableStateOf ({}) }

我可以有

val composeFF = @Composable { Text("ABC") }
val composeFunction = remember { mutableStateOf (composeFF) }

为什么我不能

val composeFunction = remember { mutableStateOf (@Composable { Text("ABC") }) }

它出错了状态

Internal Error occurred while analyzing this expression 
(Please use the "
 
" icon in the bottom-right corner to report this error):
 
jar:file:/Applications/Android%20Studio.app/Contents/lib/platform-impl.jar!/general/error.svg
Type inference failed. Expected type mismatch: inferred type is @Composable () -> Unit but () -> Unit was expected

【问题讨论】:

    标签: android function kotlin android-jetpack-compose kotlin-function-type


    【解决方案1】:

    您是否尝试过指定类型?

    val composeFunction = remember { mutableStateOf<@Composable () -> Unit> (@Composable { Text("ABC") }) }
    

    看起来编译器无法将普通函数推断为带有 @Composable 注释的东西

    【讨论】:

    • 没有为我工作..
    • @Pexers 很难说为什么,但就个人而言,这些问题是你必须寻找的关于版本和兼容性的东西,我的 kotlin 和 compose 版本几乎是最新的,有时 IDE invalidate cache~restart 会做技巧,除了类型推断之外,我不知道
    猜你喜欢
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多