-1

I've a bit of code that's inside a compose function that returns a "PscEntity" which takes (as you can notice) a code (String type) and money (Double type), I've tried to make the function return a new instance of PscEntity (becuase this function will be used to update instances of those entities) but It doesn't work due to: enter image description here and adding "@Button" doesn't let me return "PscEntity"

                Row() {
                    Button(onClick = {
                        return PscEntity(code = newCode.value, money = newMoney.value)
                    })
                    {
                        Text("Apply changes")
                    }
                    Button(onClick = {
                        return PscEntity(code = code, money = money)
                    })
                    {
                        Text("Disregard changes")
                    }

I thought of adding a variable that would be changed and then check the value with if statement but that doesn't seem like a good way to solve it,

any help will be appreciated

thanks :)

edit:

this function is called from ".callable { } "

Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .shadow(40.dp)
                    .clickable {
                        showAlertDialog("11111", 12.32)
                    }
            )
@Composable
fun showAlertDialog(code: String, money: Double): PscEntity {
    var newCode by remember { mutableStateOf(code) }
    var newMoney by remember { mutableStateOf(money) }


    Box(contentAlignment = Alignment.Center) {
        Column() {
            TextField(value = newCode, onValueChange = {
                newCode = it
            })

            TextField(value = newMoney.toString(), onValueChange = {
                newMoney = it.toDouble()
            })
            Row()
            {
                Button(onClick = {
                    return PscEntity(code = newCode, money = newMoney)
                })
                {
                    Text("Apply changes")
                }

                Button(onClick = {
                    return PscEntity(code = code, money = money)
                })
                {
                    Text("Disregard changes")
                }
            }
        }
    }
}
JustSightseeing
  • 1,460
  • 3
  • 17
  • 37

1 Answers1

1

Your code is incorrect. Compose functions do not return a result. You should use a lambda for it. Please, check sample on codeLab

Alexander
  • 511
  • 4
  • 14
  • 1
    to be honest, came back to this answer during different project just to say, for those who will see this and use return in compose functions, look at this also [link](https://developer.android.com/jetpack/compose/state) – JustSightseeing Feb 02 '22 at 22:08
  • The Compose framework itself includes composable functions that return values. e.g. `rememberCoroutineScope`. The equivalent of React hooks. – levi Dec 26 '22 at 15:26
  • It is other case. remember* functions save any state. It is not UI function. Try to find "return" in "Button", "Row", "Column" and etc – Alexander Dec 27 '22 at 14:22