skip to Main Content

I don’t know how to RETURN variable from the following function.

Here is the code…

downloadData.setOnClickListener {

            val handler = Handler(Looper.getMainLooper())
            handler.post {
                val fetchData =
                    FetchData("http://localhost/xampp/CRM/PHP/show_contacts_db.php")

                if (fetchData.startFetch()) {
                    if (fetchData.onComplete()) {
                        val result = fetchData.data.toString()
                        
                        Log.i("FetchData", result)
                        

                        val companyName = result.substringAfter("Name: ").substringBefore(";")
                        showContactName.text = "${companyName}"
                        val companyNumber = result.substringAfter("Number: ").substringBefore(";")
                        showContactNumber.text = "${companyNumber}"
                    }
                }
            }
        }

companyName and companyNumber needed to be returned so I can use it in other places.

When I Try to use Return companyNumber I have a message that "return" is not allowed here.

2

Answers


  1. Generally with lambdas, you don’t explicitly return a value – the lambda returns the value of the last expression. Using your code as an example (it won’t actually work but we’ll get to that!):

    handler.post {
        ...
        companyNumber
    }
    

    which is the same as how things like map calls take a transformation function

    listOf(1, 2, 3).map { it * 2 }
    

    that’s just doubling each number, but the result is being implicitly returned and stored in the resulting list, right? And it lets you chain lambdas together, since each one evaluates to a value (which might be Unit if it "doesn’t return a result")

    If you want, you can explicitly use what’s called a qualified return:

    handler.post {
        ...
        return@post companyNumber
    }
    

    where you’re naming the function call you’re returning to.

    Kotlin docs: returning a value from a lambda expression


    Also if you want to return two values, you can’t do that – so you’ll have to bundle them up in a single object. You could just return a Pair, or create a data class that’s more readable:

    return@post Pair(companyName, companyNumber)
    
    //or
    data class CompanyDeets(val name: String, val number: String)
    ...
    return@post CompanyDeets(companyName, companyNumber)
    

    But aside from how you do it in general, why do you want to return anything here? Handler#post takes a Runnable which returns nothing (void in Java), and View.OnClickListener#onClick doesn’t return anything either.

    Neither of them would do anything with a value you returned – and if you explicitly return a value, that means your lambda’s signature doesn’t match (right now it’s implicitly returning Unit to match what’s expected by the caller), and you’ll get an error

    What you probably want to do instead, is create a function inside your class (Activity or whatever) that uses your data, something like fun doSomethingWith(companyName: String, companyNumber: String) and call that inside your lambda. That’s way you’re executing code in reaction to a click

    Login or Signup to reply.
  2. just declare var Company Name in global, or create a function with that params

    var companyName: String? = null

    handler.post {

    companyName = result.substringAfter("Name: ").substringBefore(";")
    }

    OR

    handler.post {

    save(result.substringAfter("Name: ").substringBefore(";"))
    }

    fun save(companyName: String){ … }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search