简体   繁体   中英

Consumer can't find in Kotlin

I want to convert this Java example to kotlin.

But...

{responseBody -> .....} Type mismatch.

fun handleAudioMessageEvent(event: MessageEvent<AudioMessageContent>) {
    handleHeavyContent(
        event.replyToken,
        event.message.id
    ) { responseBody ->
        val provider = event.message.contentProvider
        val mp4: DownloadedContent
        if (provider.isExternal) {
            mp4 = DownloadedContent(null, provider.originalContentUrl)
        } else {
            mp4 = saveContent("mp4", responseBody)
        }
        reply(event.replyToken, AudioMessage(mp4.uri, 100))
    }
}
.
.
.
private fun handleHeavyContent(
    replyToken: String, messageId: String,
    messageConsumer: Consumer<MessageContentResponse>
) {
    val response: MessageContentResponse
    try {
        response = lineMessagingClient?.getMessageContent(messageId)
            ?.get()!!
    } catch (e: InterruptedException) {
        reply(replyToken, TextMessage("Cannot get image: " + e.message))
        throw RuntimeException(e)
    } catch (e: ExecutionException) {
        reply(replyToken, TextMessage("Cannot get image: " + e.message))
        throw RuntimeException(e)
    }

    messageConsumer.accept(response)
}

Type mismatch.

Required: Consumer

Found: (???) -> Unit

If you change the declaration of handleHeavyContent to this, it will work I guess:

private fun handleHeavyContent(
    replyToken: String, messageId: String,
    messageConsumer: (MessageContentResponse) -> Unit
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM