传递原始参数 可组合函数通过:使用 counterProvider: () -> Int 或使用 counter:

问题描述 投票:0回答:1

在 Jetpack Composable 中,我看到有人使用

counterProvider: () -> Int
来传递原始参数
counter: Int

我不明白上述两种方式的使用区别。

请有人为我解释一下。非常感谢你。

fun MyComposable(counter1: Int, counterProvider2: () -> Int) {
    Text("From MyComposable - Counter1 = ${counter1}", style = MaterialTheme.typography.h5)
    Text("From MyComposable - Counter2 = ${counterProvider2()}", style = MaterialTheme.typography.h5)
}
android-jetpack-compose
1个回答
0
投票

您发布的方法中的两个参数之间存在很大差异。

fun MyComposable(counter1: Int, counterProvider2: () -> Int)
  • counter1 是传递给 MyComposable 的 Int 类型的原始参数
  • counterProvider2 是一个函数,没有参数(因为括号之间没有任何内容)并返回 Int 类型

线路:

Text("From MyComposable - Counter2 = ${counterProvider2()}", style = MaterialTheme.typography.h5)

是 counterProvider2 的调用,根据它的签名,它返回一个整数。我不知道它具体做什么,但它返回一个整数。

© www.soinside.com 2019 - 2024. All rights reserved.