I am writing some pager code in jetpack compose and came to a situation where I need to change page number by button click. This is my event on button click:
onClick = {pagerState.scrollToPage(page=currentPager+1)}
but when I do this I get this error: Suspend function 'scrollToPage' should be called only from a coroutine or another suspend function
I got a solution to this by adding:
onClick = {GlobalScope.launch (Dispatchers.Main) {pagerState.scrollToPage(page=currentPager+1)}}
but still GlobalScope.launch is not recommended. Above onClick are called inside basic compose functions. How can I fix this issue in jetpack compose?