Good day everyone! This doesn’t work. Details are ...
# compose
a
Good day everyone! This doesn’t work. Details are in the thread below
Copy code
viewModel::onExampleButtonClicked
I made a wrapper on AlertDialog with the following signature
Copy code
@Composable
fun TwoButtonDialog(
    params: TwoButtonsDialog.Params,
    onPositiveClick: () -> Unit,
    onNegativeClick: () -> Unit,
) {
}
Then I call it from a Compose function like this
Copy code
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?
n
Lambda != class function. The correct syntax if you want to avoid those
{}
would be to define the vm functions as lambdas:
Copy code
val onPositiveButtonClicked: () -> Unit = { ... }
And then provide them to your composable:
Copy code
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.
a
Oh, I see. Thank you.
{}
is not ugly, I just tried to standardise the code style used in the project.
👍 2
n
@Anastasia Rozovskaya have you tried this? 👇
Copy code
TwoButtonDialog(
    params = params,
    onPositiveClick = viewModel::onPositiveButtonClicked,
    onNegativeClick = viewModel::onNegativeButtonClicked
    }
)
It should work if you have this in your VM
Copy code
class YourVm {
    fun onPositiveButtonClicked() {}
    fun onNegativeButtonClicked() {}
}
a
Using method reference here is totally valid. It looks like a bug of compose compiler plugin if it doesn't work. You can file a bug.
☝️ 2
a
Actually Nacho’s answer helped to resolve the issue. But I think I understood it wrong at first, so I just added ‘lambda defaults’ to my TwoButtonDialog aka
Copy code
@Composable
fun TwoButtonDialog(
    params: TwoButtonsDialog.Params,
    onPositiveClick: () -> Unit = {},
    onNegativeClick: () -> Unit = {},
) {
}
and used it without {} in composable screen like
onPositiveClick = viewModel::onPositiveButtonClicked
I remember how I was forced to decline this approach with ‘default lambda’. The argument was that a developer could forget to pass the implementation of this lambda in composable screen. May be I should also provide some default implementation of this lambda so that nobody will get into this trap. What do you think?
@nglauber Yeah, sure. Of course I do 🙂
Anyway, the initial suggestion, proposed by Nacho, works too.
@Albert Chang It’s really bizarre, but right now I tried to use function reference as I did yesterday and it worked. So its either a bug or a situation when you have something cached by Android Studio and this is the reason why something doesn’t work correctly.