After Upgrading to Kotlin 2.0 and Compose 1.6.10 i...
# compose
v
After Upgrading to Kotlin 2.0 and Compose 1.6.10 i am getting this error > @Composable invocations can only happen from the context of a @Composable function On this block
Copy code
supportingText = {
    errors.organisation?.let(::errorText)
        ?: {
            Text(
                text = orgLabel,
                style = MaterialTheme.typography.labelSmall,
                fontStyle = FontStyle.Italic,
                color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f
            )
        }
},
on Text, MaterialTheme.typography and MaterialTheme.colorScheme But this supporting text param is a Composable Lambda param
Copy code
supportingText: (@Composable () -> Unit)? = null,
Android Studio also not showing it as an Error in the editor
y
deleting the entire project from locale and then clone in it again has always been my solution for this problem
v
I just changed
?: { }
to
?: run {  }
and it got fixed
s
This looks like a compiler bug here, please report to Google tracker :)
🚫 1
e
Does it work if you use double brackets, i.e
?: {{ }}
s
I think you want to add @Composable before {} to fix it, but it should be inferred by the compiler
Actually, looking at the snippet again, I am not sure what's the intention here
v
It was working before upgrading to 2.0 Ill report the bug in Google Issue Tracker
The intention is Just to show an error text if there is else show a supporting text Its designed like this because the
errorText
composable is reused among multiple text fields
s
The thing is that this lambda is never executed, it seems like
1
v
Couldn't understand It is executed and is displayed correctly The error was shown when building
e
@shikasd is saying that it's never executed, because it's already in the
supportingText
lambda. So if
errors.organization
is null, you're just creating the
orgLabel
lambda, but never running it. You probably want this (the same thing you posted but with the outer brackets removed)
Copy code
supportingText = errors.organisation?.let(::errorText)
         ?: {
             Text(
                 text = orgLabel,
                 style = MaterialTheme.typography.labelSmall,
                 fontStyle = FontStyle.Italic,
                 color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.7f
             )
         }
v
@eygraber that makes sense There was no compile time error before but there is now, That looks like an improvement and not a bug Thanks for helping out
I added the brackets to make it look readable not knowing that it completely changes what it does