Hello! I'm trying to call an api on button click b...
# multiplatform
s
Hello! I'm trying to call an api on button click but am facing issues with it. Can anyone help me figure out how I can do that?
1
p
What problem you are having when you call it?
s
I was trying to call the getResponse function within the onClick() like this
But getting the error "@Composable invocations can only happen from the context of a @Composable function", so not sure how to go about calling that function on button click
p
You are calling the rememberCouroutibe function which is mark as composable from the onClick lambda scope which is not. Move it out to the box scope which is Composable scope
Pull everything out from onClick lambda and put in box content
s
Ah okay, and then how can I link it to onClick/call the getResponse when the button is clicked?
p
scope.launch
you can leave it inside the onClick. You don't need the launchEffect
s
Awesome, got it to work! Thank you so much!!
p
I would advise trying to use some state management library to help with this type of things. Either compose navigation if you are only on Android or Voyager if doing multiplatform.