Hello all! I wanted some help in KMM for calling a...
# getting-started
s
Hello all! I wanted some help in KMM for calling a get API on button click. I've been trying different things but I keep getting errors such as '@composable invocations can only happen from the context of a @composable function' or 'suspend function 'onclick' should be called only from a coroutine or another suspend function' and I'm not sure what the correct way to do this is. Is there any sample on github or any latest tutorial I can follow for this? For reference, I have followed

this video

for creating a button, and am trying to add on to that by having an API call set up according to

this video

, but am unable to find something that combines both. Any help would be really appreciated, I've been stuck on this for a while now 😞
j
Calling a
suspend
function from Compose, you should use
rememberCoroutineScope()
to get a
CoroutineScope
bound to the composable's lifecycle. Then launch a coroutine and call the
suspend
function in it. See docs. You should also become familiar with coroutines and how they work. As for the other error:
@composable invocations can only happen from the context of a @composable function
Are you trying to call a function annotated with
@Composable
from within your
onClick
handler? Composable functions should only be used to compose your UI (or within other compose hierarchies). You shouldn't be calling from an event handler.