Kotlin 如何将可为空的FlowTable转换为LiveData

Kotlin 如何将可为空的FlowTable转换为LiveData,kotlin,Kotlin,在我的repository类中,我调用webservice。如果查询不为null,则返回可流动的结果;如果查询为null,则返回null。然后在我的ViewModel中,我将可流动数据转换为LiveData 下面是代码片段: 在myRepository类中: fun fetchToDosFromServer(fullQueryString: String?) : Flowable<GitResult>? { if(fullQueryString.length

在我的repository类中,我调用webservice。如果查询不为null,则返回可流动的结果;如果查询为null,则返回null。然后在我的ViewModel中,我将可流动数据转换为LiveData

下面是代码片段:

在myRepository类中:

fun fetchToDosFromServer(fullQueryString: String?)
        : Flowable<GitResult>? 
{
    if(fullQueryString.length>0)
    { //call webservice- which return observable<GitResult>
       var returnedResult = myWebServiceCall()
        return returnedData.toFlowable(BackpressureStrategy.BUFFER)
       }

    else 
        return null

}
 fun getReposFromServer()
 {
    val resultFromApiCall_flowable : Flowable<GitResult>? =  mainRepository.fetchToDosFromServer("q=2")

    val source: LiveData<GitResult> = LiveDataReactiveStreams.fromPublisher(resultFromApiCall_flowable)
 }
fun fetchToDosFromServer(fullQueryString:String?)
:可流动?
{
如果(fullQueryString.length>0)
{//调用webservice-返回可观察的
var returnedResult=myWebServiceCall()
将returnedData.toFlowable返回(背压等级缓冲区)
}
其他的
返回空
}
在myViewModel类中:

fun fetchToDosFromServer(fullQueryString: String?)
        : Flowable<GitResult>? 
{
    if(fullQueryString.length>0)
    { //call webservice- which return observable<GitResult>
       var returnedResult = myWebServiceCall()
        return returnedData.toFlowable(BackpressureStrategy.BUFFER)
       }

    else 
        return null

}
 fun getReposFromServer()
 {
    val resultFromApiCall_flowable : Flowable<GitResult>? =  mainRepository.fetchToDosFromServer("q=2")

    val source: LiveData<GitResult> = LiveDataReactiveStreams.fromPublisher(resultFromApiCall_flowable)
 }
fun getReposFromServer()
{
val resultfrompicall_flowable:flowable?=mainRepository.fetchToDosFromServer(“q=2”)
val source:LiveData=LiveDataReactiveStreams.fromPublisher(resultfrompicall\u可流动)
}
但当试图在myViewModel类中将flowable转换为LiveData时,它会显示编译时错误。行中:

LiveDataReactiveStreams.fromPublisher(结果可流动)

错误是:

错误:需要可流动,找到可流动

如何解决它?

您的流程是可选的

因此,如果您进行空检查,它应该可以工作

val resultFromApiCall_flowable: Flowable<GitResult>? = mainRepository.fetchToDosFromServer("q=2")

if (resultFromApiCall_flowable != null) {
 val source: LiveData<GitResult> = LiveDataReactiveStreams.fromPublisher(resultFromApiCall_flowable)
}

从marstran那里你已经得到了如何直接处理的答案,但我建议你考虑另一个方向。简单地说,不要使结果为空,在这种情况下,返回
Flowable.empty()
。而且可能也不接受
String?
;在某些情况下,是否确实要调用
fetchToDosFromServer(null)

@coroutineDispatcher:dot不起作用
val resultFromapcall\u flowable:flowable?=mainRepository.fetchToDosFromServer(“q=2”)
是可选的。因此,您可以使用
val resultfrompicall\u flowable:flowable!=或进行空检查
resultfrompicall_flowable?.let{val source:LiveData=LiveDataReactiveStreams.fromPublisher(it)}
@kuzdu:我使用了第二个选项,它成功了。谢谢但我还有一个困惑。。如果我使用
if(resultfrompicall\u flowable!=null){val source:LiveData=LiveDataReactiveStreams.fromPublisher(resultfrompicall\u flowable)}
则错误也会消失。但是它与
xx?相同。让{…}
它是,但是您可以在这里更方便地处理
其他
。如果我返回
Flowable.empty()
那么当它返回空时,我如何检查?在大多数情况下,您不想检查;只需将其视为任何其他返回的
可流动的
。如果您确实想区分这种情况,这不是一个好的解决方案。但是为什么对待
fetchToDosFromServer(“”
fetchToDosFromServer(anyInvalidQueryString)
不同呢?当然,这建议返回
Flowable.error()
或抛出异常,而不是
Flowable.empty()
fetchToDosFromServer
对无效查询字符串执行的任何操作。