Home > Back-end >  Why can't I remember mutableStateOf Composable Function directly?
Why can't I remember mutableStateOf Composable Function directly?

Time:10-08

I can have

val composeFunction = remember { mutableStateOf ({}) }

I can have

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

Why can't I have

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

It errors out state

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

CodePudding user response:

Have you tried specifying the type?

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

Looks like the compiler cannot infer an ordinary function to something that is supplied with a @Composable annotation

CodePudding user response:

@Composable invocations can only happen from the context of a @Composable function. So you can't have this annotation inside the mutableStateOf().

  • Related