【发布时间】:2021-09-04 01:17:20
【问题描述】:
这里我有 i 函数来获取一些数据。我使用 Either 向 ViewModel 发送数据。
sealed class Either<out L, out R> {
/** * Represents the left side of [Either] class which by convention is a "Failure". */
data class Left<out L>(val a: L) : Either<L, Nothing>()
/** * Represents the right side of [Either] class which by convention is a "Success". */
data class Right<out R>(val b: R) : Either<Nothing, R>()
}
如何在 catch 块中发出错误数据?
fun getStocksFlow(): Flow<Either<Throwable, List<Stock>>> = flow {
val response = api.getStocks()
emit(response)
}
.map {
Either.Right(it.stocks.toDomain())
}
.flowOn(ioDispatcher)
.catch { throwable ->
emit(Either.Left(throwable)) //Here it shows Type mismatch, it needs Either.Right<List<Stock>>
}
【问题讨论】:
标签: android kotlin flow either