Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Flatmap无法向右投射_Android_Kotlin_Rx Java - Fatal编程技术网

Android Flatmap无法向右投射

Android Flatmap无法向右投射,android,kotlin,rx-java,Android,Kotlin,Rx Java,我试图在我的登录函数中转换一个可观察的对象,但我一直得到这个错误。这是我在平面地图上看到的代码和错误 fun login(phoneNumber: String, password: String, deviceId: String) { // remove previous subscriptions disposables.clear() // function to save userInfo and access token

我试图在我的登录函数中转换一个可观察的对象,但我一直得到这个错误。这是我在平面地图上看到的代码和错误

fun login(phoneNumber: String, password: String, deviceId: String) {
        // remove previous subscriptions
        disposables.clear()

        // function to save userInfo and access token
        val saveResponse = { response: LoginResponse ->
            val user = response?.user
            val token = response?.token
//            userManager.updateToken(champion, token, deviceId)
        }

        // on success callback
        val onSuccess = { isSuccess: Boolean ->
            progressBarVisibility.postValue(false)
            loginSuccess.postValue(isSuccess)
            if (!isSuccess) errorMessage.postValue("An error occurred please try again.")
        }

        // on failure callback
        val onError = { throwable: Throwable ->
            val message = when (throwable) {
                is HttpException -> when (throwable.code()) {
                    400 -> "Enter valid Phone Number or Password"
                    422 -> "Incorrect Phone Number or Password"
                    else -> throwable.toErrorMessage()
                }

                else -> "An Error Occurred."
            }

            // show error message
            errorMessage.postValue(message)
            progressBarVisibility.postValue(false)
        }

        val disposable = accountUseCase.login(LoginRequest(phoneNumber, password)).observeOnUI()
                .doOnSubscribe { progressBarVisibility.postValue(true) }
                .flatMap {
                    val resp = it.data
                    when (resp) {
                        null -> Single.just(false)
                        else -> saveResponse(it)
                    }
                }
                .subscribe(onSuccess, onError)

        // add subscription to disposables
        disposables.add(disposable)
    }
错误

类型不匹配。必需:((基本响应!)→ 单一来源!)!找到:(BaseResponse!)→ 任何

问题在于
平面图的隐式返回:

when (resp) {
    null -> Single.just(false)
    else -> saveResponse(it)
}
null
分支中,返回类型为
Single

else
分支中,返回
saveResponse
的结果。但是
saveResponse
的返回类型是
Unit
,因为它不返回任何内容

因此,Kotlin推断出
flatMap
的返回类型为
单个
单元
,其中唯一常见的超类型是
任何

这就是为什么会出现错误消息:
Found:(BaseResponse!)→ 任何


您需要让
saveResponse
返回某些内容,可能还需要一个
单个
,这取决于您的用例。

返回单个时,
saveResponse
返回什么?
when (resp) {
    null -> Single.just(false)
    else -> saveResponse(it)
}