'Type mismatch. Required: (PaddingValues) → Unit Found: Unit [duplicate]

I'm trying to display a profile screen using:

Scaffold(
    topBar = {
        DarkTopBar()
    },
    content = ProfileContent()
)

Where the ProfileContent() looks like this:

@Composable
fun ProfileContent() {
    Box(
        modifier = Modifier.fillMaxSize().padding(top = 96.dp),
        contentAlignment = Alignment.TopCenter
    ) {
        Text(
            text = "Good day!",
            fontSize = 48.sp
        )
    }
}

But I get the following error:

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

What I have tried to solve this problem is to move the above function call inside the body:

Scaffold(
    topBar = {
        DarkTopBar()
    }
) {
    ProfileContent() //Moved here.
}

But Android Studio is complaining saying:

Content padding parameter it is not used

Can anyone help me?



Solution 1:[1]

Your problem is that the content parameter of Scaffold should take a composable function. Here, you are not passing the function as an argument as you think you are, but instead actually calling the function. To fix put the function call inside a lambda for content like you did for topAppBar.

So change this:

Scaffold(
    topBar = {
        DarkTopBar()
    },
    content = ProfileContent()
)

To this:

Scaffold(
    topBar = {
        DarkTopBar()
    },
    content = {
        ProfileContent()
    }
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Can_of_awe