Hello :slightly_smiling_face: what’s the best way ...
# compose
t
Hello 🙂 what’s the best way to access a string resource inside a coroutine scope? Code in a 🧵
Example problem
Copy code
Button(onClick = {
  coroutineScope.launch {
    # here you can't do stringResource cause of 
    # @Composable invocations can only happen from the context of a @Composable function
    snackbarHostState.showSnackbar(stringResource(id = R.string.clicked))
  }
}) {
  Text(stringResource(id = R.string.click))
}
How I currently solve the problem
Copy code
val clickedText = stringResource(id = R.string.clicked)

Button(onClick = {
  coroutineScope.launch {
    snackbarHostState.showSnackbar(clickedText)
  }
}) {
  Text(stringResource(id = R.string.click))
}
a
This is the best (and only) way.
🙌 1
☝️ 1
a
Not the “only”, but agree it’s best 😄
Copy code
val context = LocalContext.current
Button(onClick = { context.getString(...) })