如何将 Flow<List<Object>> 转换为 Flow<Object> Kotlin 协程?
How to convert Flow<List<Object>> to Flow<Object> Kotlin coroutine?
给定一个 ObjectA
,其中包含来自 WebClient
请求的 ObjectB
的 List
。我想 return 我的 DTO
来自 ObjectB
data class MyDto(val id: String, val name: String) {
companion object {
fun from(objectB: ObjectB) {
return MyDto(id = objectB.id, name = objectB.name)
}
}
...
return webClient.get()
.uri(baseUrl)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToFlow<ObjectA>()
.flatMapConcat { response ->
val x = response.objectB.map { MyDto.from(it) }
retrun@flatMapConcat flowOf(x)
}
Array<Array<MyDto>>
的returnJSON
[
[
{
"id": 1,
"name": "Joe"
},
{
"id": 2,
"name": "Jane"
}
]
]
预期结果是Flow<MyDto>
[
{
"id": 1,
"name": "Joe"
},
{
"id": 2,
"name": "Jane"
}
]
flowOf(x)
returns Flow<List<ObjectA>>
, 但你需要 return Flow<ObjectA>
...
.flatMapConcat { response ->
response.objectB.asFlow().map { MyDto.from(it) }
}
给定一个 ObjectA
,其中包含来自 WebClient
请求的 ObjectB
的 List
。我想 return 我的 DTO
来自 ObjectB
data class MyDto(val id: String, val name: String) {
companion object {
fun from(objectB: ObjectB) {
return MyDto(id = objectB.id, name = objectB.name)
}
}
...
return webClient.get()
.uri(baseUrl)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToFlow<ObjectA>()
.flatMapConcat { response ->
val x = response.objectB.map { MyDto.from(it) }
retrun@flatMapConcat flowOf(x)
}
Array<Array<MyDto>>
[
[
{
"id": 1,
"name": "Joe"
},
{
"id": 2,
"name": "Jane"
}
]
]
预期结果是Flow<MyDto>
[
{
"id": 1,
"name": "Joe"
},
{
"id": 2,
"name": "Jane"
}
]
flowOf(x)
returns Flow<List<ObjectA>>
, 但你需要 return Flow<ObjectA>
...
.flatMapConcat { response ->
response.objectB.asFlow().map { MyDto.from(it) }
}