Home > OS >  Why Kotlin code unreachable in my project?
Why Kotlin code unreachable in my project?

Time:12-01

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?

CodePudding user response:

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 returns and change the return type of your functions with the actual types you are returning. e.g.: (I've assumed String a return type)

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