首页 > 解决方案 > 如何使用 Kotlin 协程创建 Flow 请求

问题描述

我在我的应用程序中使用 Retrofit 和 Coroutines。

所以我的应用程序中有两个不同的请求。我想将它们与协程的 combine 结合起来。如果我想将两个请求相互结合,我需要它们是Flow. 但是当我将我的 API 请求定义为 Flow 时,我会收到一个错误:

   @GET("my_url")
   fun getHomeDataFlow(@Header("Authorization") token: String): Flow<Pagination<Home>>

错误:

java.lang.IllegalArgumentException: Unable to create call adapter for kotlinx.coroutines.flow.Flow<codenevisha.ir.app.journey.data.model.response.Pagination<codenevisha.ir.app.journey.data.model.response.Home>>
        for method APIService.getHomeDataFlow

这也是我的请求实现:

 val homeFlow = oldRepository.getHomeDataFlow(params)
        val categoryFlow = oldRepository.getCategoriesFlow()
        var result = Pagination<Home>()

        homeFlow.combine(categoryFlow) { receivedHome, receivedCategory ->

            val home = Home()
            home.setCategory(receivedCategory)
            home.showType = Home.ShowType.CATEGORY.id
            receivedHome.results?.add(1, home)
            receivedHome

        }.collect {
            result = it
        }

标签: androidretrofitkotlin-coroutines

解决方案


推荐阅读