Hi, everyone! Recently, i started to develop web a...
# ktor
Hi, everyone! Recently, i started to develop web app using vaadin framework. My aim is to fetch data from REST API. I tried use ktor client for this, but i have the next problem: i need to invoke suspend function in non suspend context(variable declaration). This is my view code:
Copy code
class MainView : KComposite() {
    private val apiClient: ScenarioServiceClient = ScenarioServiceClientImpl("<http://localhost:5000>");
    private val root = ui {
        verticalLayout {
            horizontalLayout {
                    .forEach { scenario -> scenarioItemView(scenario) }
This is how i use ktor:
Copy code
class ScenarioServiceClientImpl(private val baseUrl: String) : ScenarioServiceClient {
    private val restClient: HttpClient = HttpClient(Apache)
    override suspend fun getAvailableScenarios() : List<ScenarioViewModel>{
        val scenarios = restClient.get<List<ScenarioViewModel>>("${baseUrl}/scenarios")}}
May be there is some way to invoke api call without suspend?
Hi, I don't know Vaadin, so I'll give you some ingredients. 1. you need to have a coroutine scope bound to your MainView so that when it is destroyed the associated coroutine scope's job will be canceled (and with it, all the pending calls to your API) 2. then you can add a
var availableScenarios: List<ScenarioViewModel>? = null
3. you can then add an
init { launch { availableScenarios = apiClient.getAvailableScenarios() } }
block 4. finally, in your
, you just have to test whether the
is null or not, and display either a loading widget or the result. All this will only work, if you can dynamically add children to your
tree and that, somehow, KComposite rebuilds it view. Hope this helps.
­čĹŹ 1
You can find this article helpful.
thx guys!