简体   繁体   中英

Assignments are not expressions, and only expressions are allowed in this context - Kotlin

bindingSub.btnCoinup.setOnClickListener {
    fbFirestore?.collection("users")?.document(fbAuth?.uid.toString())?.get()?.addOnSuccessListener { document ->
        if (document != null) {
            var autoCoin = document.data?.values.toString().replace("[", "").replace("]", "").toLong()
            bindingSub.tvAutoCoin.setText("Coin: ${autoCoin += 1}")
        } else {
            bindingSub.tvAutoCoin.setText("Coin: 0")
        }
    } ?.addOnFailureListener { exception ->
        ...

bindingSub.tvAutoCoin.setText("Coin: ${autoCoin += 1}")

I tried really hard to fix this error, but I couldn't fix it.

Even if the code is dirty, I would appreciate it if you understand...

You are assigning a new value in this line to the autoCoin variable:

bindingSub.tvAutoCoin.setText("Coin: ${autoCoin += 1}")

String templates only allow expressions. An expression is something that results in a value of some type, even if that type is Unit and can be on the right hand side of an assignment statement (eg val x = 3 assigns the value of the expression 3 to x ). Some languages (like Java or C++) assignments are expressions, but in Kotlin they are not.

The x += 1 operator is a special shorthand syntax for x = x + 1 .

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