What am I doing wrong when using LET (Kotlin)?

1

I'm using retrofit and in onReaponse, when I try to get body information with (Let), the internal code does not execute.

response?.body()?.let {
  Log.i("Info", "Cidades: " + it.size)
}

If you try like this, it also does not work:

response?.body()?.let {
  Log.i("Info", "Cidades: " + it.size)
}

But if I put it like this, it works:

if (response != null) {
  if (response.body() != null){
     Log.i("Info", "Cidades: " + response.body()?.size)
  }
}

What am I missing? The whole code is below.

call.enqueue(object: Callback<List<Cidade>?> {
    override fun onResponse(call: Call<List<Cidade>?>?, response: Response<List<Cidade>?>?) {
        /*Not working - Does Not execute Log*/
        response?.let {
            Log.i("Info", "Cidades: " + it.body()?.size)
        }

        /*Working - Does execute Log*/
        if (response != null) {
            if (response.body() != null){
                Log.i("Info", "Cidades: " + response.body()?.size)
            }
        }
    }

    override fun onFailure(call: Call<List<Cidade>?>?, t: Throwable?) {
        var error = t?.message.toString()
        Log.e("error", error)

        txtErrorMessage.text = error
        txtErrorMessage.visibility = View.VISIBLE
    }
})
    
asked by anonymous 09.07.2018 / 15:14

0 answers