Hi Everyone, why i`m facing this error @Composable invocations can only happen from the context of a...
a
Hi Everyone, why i`m facing this error @Composable invocations can only happen from the context of a @Composable function at stringResource(R.string.dont_have_an_account) line?
Copy code
derivedStateOf {
    buildAnnotatedString {
        withStyle(
            style = SpanStyle(
                color = PrimaryTextColor,
                fontSize = TextUnit(12f, TextUnitType.Sp),
                fontFamily = FontFamily(listOf(Font(R.font.poppins_regular)))
            )
        ) {
            append(stringResource(R.string.dont_have_an_account))
        }
        withStyle(
            style = SpanStyle(
                color = BlueColor,
                fontSize = TextUnit(12f, TextUnitType.Sp),
                fontFamily = FontFamily(listOf(Font(R.font.poppins_semibold)))
            )
        ) {
            append("  Create Here")
        }
    }
}
f
Copy code
stringResource
is a composable function and you're not in a compose scope. Either read the string first and keep it in a variable, or keep Localcontext.current in a variable and then use getString on that
a
Like this :
Copy code
val context = LocalContext.current
append(context.resources.getString(R.string.dont_have_an_account))
o
I thought it was a bug in Compose, that was fixed? https://issuetracker.google.com/issues/218690200
a
My solution is right or not? as its also working…
@Oleksandr Balan i use buildAnnotatedString inside of derivedStateOf that`s why i have to use it using this:
Copy code
val context = LocalContext.current
append(context.resources.getString(R.string.dont_have_an_account))
o
Yea, I got it In this case you have to use
LocalContext.current
like you did 🤷
a
Thanks @Francesc, @Oleksandr Balan
2756 Views