简体   繁体   中英

Why Kotlin code unreachable in my project?

I have simple model and I want to use user profil image and name, but it is throw an error like "Unreachable code" for

participants.find { it.id.= userId }!!.profilePicture

and

participants.find { it.id.= userId }!!.username

class Talk(
    val participantsId: ArrayList<String>

): BaseModel(), Serializable {
    var participants = ArrayList<Data>()
    val messages = ArrayList<Message>()

    fun getProfilePicture(userId: String) {
        return
            participants.find { it.id != userId }!!.profilePicture

    }

    fun getTalkName(userId: String) {
        return
            participants.find { it.id != userId }!!.username

    }
}

any idea?

Since in Kotlin lines don't need to end with a column ( ; ), return followed by a new line is interpreted as a return with no return value (equivalent to return; in Java).

You just need to remove the new lines after the return s and change the return type of your functions with the actual types you are returning. eg: (I've assumed String a return type)

fun getTalkName(userId: String): String {
    return participants.find { it.id != userId }!!.username
}

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