简体   繁体   English

Jetpack Compose Kotlin 说

[英]Jetpack Compose Kotlin says

I'm passing a lambda to a composable to trigger on Modifier.clickable but the compiler warns that "The expression is unused"我将 lambda 传递给可组合以在 Modifier.clickable 上触发,但编译器警告“表达式未使用”

Why would this be happening and how to correct it?为什么会发生这种情况以及如何纠正它? Very weird?很奇怪?

Screenshot of compiler warning编译器警告的屏幕截图


@Composable
fun MainScreen(dateVM: DateViewModel = DateViewModel(), dateList: List<DateList>) {

    val dayIndexState = remember {
        mutableStateOf(0)
    }

    Column {
        ShowDateScroll(dateIndex = dayIndexState.value) {
            newDateIndex -> dayIndexState.value = newDateIndex
        }
    }
}

@Composable
fun ShowDateScroll(
    dateIndex: Int,
    dateIndexUpdate: (newIndexState: Int) -> Unit
) {
    LazyRow(
        modifier = Modifier
            .fillMaxWidth()
            .background(Color.LightGray),
        horizontalArrangement = Arrangement.spacedBy(25.dp)
    ) {
        items(items = dateList, itemContent = { dateItem ->
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .clickable(
                        role = Role.Button
                    ) {
                        dateIndexUpdate
                    }
            ) {
                Text(
                    text = dateItem.dow
                )
                Text(
                    text = dateItem.day.toString(),
                )
            }
        })
    }
}

the code you passed in the trailing lambda will never execute.您在尾随 lambda 中传递的代码将永远不会执行。

A trailing lambda itself is a function, if you just write down another function name nothing will happen.尾随的 lambda 本身就是一个函数,如果你只是写下另一个函数名,什么都不会发生。 Compare to:相比于:

fun function() {}
fun other() {
  function
}

If you never use the function / invoke it.如果您从不使用该函数/调用它。 Thus it is unused.因此它未被使用。

In order to invoke the code you have different options: Replace the trailing lambda with the function reference为了调用代码,您有不同的选择:用函数引用替换尾随的 lambda

Modifier.clickable(
  role = Role.Button,
  onClick = dateIndexUpdate,
)

Another way is to invoke the function:另一种方法是调用函数:

Modifier.clickable(
  role = Role.Button,
) {
  dateIndexUpdate()
  /* or */
  dateIndexUpdate.invoke()
}

In your code the second option can be used with clickable because you need to provide the parameter:在您的代码中,第二个选项可以与 clickable 一起使用,因为您需要提供参数:

Modifier.clickable(
  role = Role.Button,
) {
  dateIndexUpdate(dateIndex)
  /* or */
  dateIndexUpdate.invoke(dateIndex)
}

Not sure if it is the correct index, you need to adjust it for your code.不确定它是否是正确的索引,您需要为您的代码调整它。

Because you are just referencing the method.因为你只是在引用方法。 You must invoke it to use it.您必须调用它才能使用它。 Like dateIndexUpdate(...) .dateIndexUpdate(...) Add the new index state in the parameter, or whatever your use case is在参数中添加新的索引状态,或者无论您的用例是什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM