Anastasia Rozovskaya
08/19/2021, 12:33 PMviewModel::onExampleButtonClicked
Anastasia Rozovskaya
08/19/2021, 12:34 PM@Composable
fun TwoButtonDialog(
params: TwoButtonsDialog.Params,
onPositiveClick: () -> Unit,
onNegativeClick: () -> Unit,
) {
}
Then I call it from a Compose function like this
TwoButtonDialog(
params = params,
onPositiveClick = {
viewModel.onPositiveButtonClicked()
},
onNegativeClick = {
viewModel.onNegativeButtonClicked()
}
)
This works fine. But, I’d like to use a function reference for this no arg functions from view model, but in attempt to do so I’m getting this error
java.lang.NoSuchMethodError: No static method TwoButtonDialog … in class …TwoButtonsDialogKt; or its super classes
What am I missing? Why doesn’t it work?Nacho Ruiz Martin
08/19/2021, 12:56 PM{}
would be to define the vm functions as lambdas:
val onPositiveButtonClicked: () -> Unit = { ... }
And then provide them to your composable:
TwoButtonDialog(
params = params,
onPositiveClick = viewModel.onPositiveButtonClicked,
onNegativeClick = viewModel.onNegativeButtonClicked
)
I sincerely feel that {}
is not that ugly since you quickly see that there’s a function invocation happening there.Anastasia Rozovskaya
08/19/2021, 1:09 PM{}
is not ugly, I just tried to standardise the code style used in the project.nglauber
08/19/2021, 1:30 PMTwoButtonDialog(
params = params,
onPositiveClick = viewModel::onPositiveButtonClicked,
onNegativeClick = viewModel::onNegativeButtonClicked
}
)
nglauber
08/19/2021, 1:31 PMclass YourVm {
fun onPositiveButtonClicked() {}
fun onNegativeButtonClicked() {}
}
Albert Chang
08/19/2021, 1:35 PMAnastasia Rozovskaya
08/20/2021, 9:35 AM@Composable
fun TwoButtonDialog(
params: TwoButtonsDialog.Params,
onPositiveClick: () -> Unit = {},
onNegativeClick: () -> Unit = {},
) {
}
and used it without {} in composable screen like
onPositiveClick = viewModel::onPositiveButtonClicked
Anastasia Rozovskaya
08/20/2021, 9:38 AMAnastasia Rozovskaya
08/20/2021, 9:40 AMAnastasia Rozovskaya
08/20/2021, 10:15 AMAnastasia Rozovskaya
08/20/2021, 4:02 PM