Home > Software engineering >  Kotlin: Why am I getting a compile error cannot find a parameter for modifier and contentScale?
Kotlin: Why am I getting a compile error cannot find a parameter for modifier and contentScale?

Time:09-23

I am getting the error messages "Cannot find a parameter with this name: modifier" and "Cannot find a parameter with this name: contentScale". The compiler is not giving me an option to import anything when I hover on the parameters. How can I resolve this?

@Composable
fun BirthdayGreetingWithImage(message: String, from: String) {
    val image = painterResource(
    id = R.drawable.androidparty,
    modifier = Modifier.fillMaxHeight().fillMaxWidth(),
    contentScale = ContentScale.Crop
)

CodePudding user response:

modifier and contentScale needs to be used on an image, not on the painterResource itself. Try

Image(
    painterResource(R.drawable.androidparty),
    modifier = Modifier.fillMaxHeight().fillMaxWidth(),
    contentScale = ContentScale.Crop
)

CodePudding user response:

It happens because exists a Box constructor with no content as in your example code:

@Composable
fun Box(mModifier: Modifier): Unit

The contentViewAlignment doesn't exist in this constructor.

You can use the constructor with the contentViewAlignment parameter and in this case you have to pass also the content parameter:

@Composable
inline fun Box(
    modifier: Modifier = Modifier,
    contentViewAlignment: Alignment = Alignment.TopStart,
    propagateMinConstraints: Boolean = false,
    content: @Composable @ExtensionFunctionType BoxScope.() -> Unit
): Unit

For example:

Box(
    mModifier = Modifier,
    contenViewtAlignment = Alignment.Center
){
    //content
}
  • Related