Patricia Tapia
Patricia Tapia

Reputation: 113

Preview in Jetpack Compose with lambda function doesn't work

I am trying to learn Jetpack compose but I have an issue with the preview. I have this composable.

fun RegistrationScreen(
    state: RegisterState,
    onRegister: (String, String, String, String) -> Unit,
    onBack: () -> Unit,
    onDismissDialog: () -> Unit
) { //Code }
@Preview(showBackground = true)
@Composable
private fun DefaultPreview() {
    RegistrationScreen(
        state = RegisterState(),
        onRegister = "Name",
        onBack = { },
        onDismissDialog = { }
    )
}

Obviously this is the problem in onRegister

Type mismatch: inferred type is String but (String, String, String, String) -> Unit was expected

But I can't pass these Strings parameters together and I don't know why.

For extra context, this is my NavGraphBuilder:

fun NavGraphBuilder.addRegistration(
    navController: NavHostController
){
    composable(route = Destinations.Register.route) {
        val viewModel: RegisterViewModel = hiltViewModel()
        RegistrationScreen(
            state = viewModel.state.value,
            onRegister = viewModel::register,
            onBack = {
                navController.popBackStack()
            },
            onDismissDialog = viewModel::hideErrorDialog
        )
    }

And this my ViewModel:

class RegisterViewModel : ViewModel() {
    val state: MutableState<RegisterState> = mutableStateOf(RegisterState())
    fun register(
        name: String,
        email: String,
        password: String,
        confirmPassword: String
    ) { //code }

Upvotes: 7

Views: 1764

Answers (1)

Massimiliano
Massimiliano

Reputation: 190

You have to write something like this to make it works

@Preview(showBackground = true)
@Composable
private fun DefaultPreview() {
    RegistrationScreen(
        state = RegisterState(),
        onRegister = { _, _, _, _ -> },
        onBack = { },
        onDismissDialog = { }
    )
}

Upvotes: 13

Related Questions