Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Preview Jetpack Compose

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 }

Thanks for the help.

>Solution :

You have to write something like this to make it works

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading