https://kotlinlang.org logo
Title
d

Dawid Hyzy

02/10/2020, 1:15 AM
I would like to ask regarding setter and getter, I have piece of code like that. Whenever I tried to access the variable listToko on another class, it always return empty string, while when I println resp, I know there's the value inside resp. Is there something wrong with how I use setter / getter ? Thanks
d

deactivateduser

02/10/2020, 2:09 AM
Hi, you are always creating new object of getList inside your callback. Hence, when you try to print it out outside, you will be using a different object as well. Try to print the listToko value inside your caback. Suppose it should not be empty.
d

Dawid Hyzy

02/10/2020, 2:45 AM
Hi Handra, thanks for helping. I'm sorry but I still don't understand how to print inside my callback, could you give me further hint if you don't mind ? Thanks
d

deactivateduser

02/10/2020, 3:09 AM
The callback I'm referring to is the callback called upon the successful call of your HTTP (i.e.: onResponse). Remember that getList is a class, albeit it actually does not follow proper naming convention. So, the statement below:
val res = getList()
is actually creating an object of getList named res. And, you are doing it inside your onResponse function. Now, I believe when you want to print the value of listToko outside the class, you will do the same declaration as above. This means, you are actually creating a new object of getList. Hence, your listToko is empty as it is the default. You can do something like below and see that your listToko is actually not empty inside your callback function.
class getList{
    open var listToko: String? = ""
}

var abc = client.newCall(post).enqueue(object: Callback {
            override fun onResponse(call: Call, response: Response) {
                if(response.isSuccessful){
                    val resp = response.body?.string()
                    val res = getList()
                    res.listToko = resp
                    println ( res.listToko )
                }
            }
}
d

Dawid Hyzy

02/10/2020, 3:22 AM
Yep, you got it right, I want to use the value of listToko on other class. Also, the result of println res.listToko isn't empty here's the value
[{"kodebagian":"TRIE"},{"kodebagian":"TUOL"}]
d

deactivateduser

02/10/2020, 3:27 AM
I'm not sure your class design, but what I can advise is for you to pass in the object of getList from another class and set the value of listToko of that object inside your callback instead of keep creating a new object of getList everytime inside your onResponse. Another thing is follow proper coding convention. This might be a good start for you https://kotlinlang.org/docs/reference/coding-conventions.html#naming-rules.
d

Dawid Hyzy

02/10/2020, 3:28 AM
So in other words, I should pass the value of res.listToko as parameter to other class ?
d

deactivateduser

02/10/2020, 3:30 AM
You should pass your res object, not your listToko. Another way is to make a function that returns the getList object, which I think is a better approach.
d

Dawid Hyzy

02/10/2020, 3:31 AM
Ah right, I got it. Thanks for your help. Also thanks for the resource, I will look into it. Once again, thanks for helping me