https://kotlinlang.org logo
#compose
Title
# compose
a

Asad Mukhtar

08/17/2022, 6:22 AM
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

Francesc

08/17/2022, 6:34 AM
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

Asad Mukhtar

08/17/2022, 6:35 AM
Like this :
Copy code
val context = LocalContext.current
append(context.resources.getString(R.string.dont_have_an_account))
o

Oleksandr Balan

08/17/2022, 6:36 AM
I thought it was a bug in Compose, that was fixed? https://issuetracker.google.com/issues/218690200
a

Asad Mukhtar

08/17/2022, 6:37 AM
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

Oleksandr Balan

08/17/2022, 6:41 AM
Yea, I got it In this case you have to use
LocalContext.current
like you did 🤷
a

Asad Mukhtar

08/17/2022, 6:42 AM
Thanks @Francesc, @Oleksandr Balan
1308 Views