我正在嘗試將 MutableState 變數傳遞給另一個函式。下面的一切都很好。但我不喜歡myMutableState.value
@Composable
fun Func() {
val myMutableState = remember { mutableStateOf(true) }
Column {
AnotherFunc(mutableValue = myMutableState)
Text(if (myMutableState.value) "Stop" else "Start")
}
}
@Composable
fun AnotherFunc(mutableValue: MutableState<Boolean>) {
}
所以我決定使用val myMutableState by remember { mutableStateOf(true) },如下圖。我不再需要使用myMutableState.value如下圖。
不幸的是,以下內容無法編譯。這是因為我不能將它傳遞給函式AnotherFunc(mutableValue = myMutableState)
@Composable
fun Func() {
val myMutableState by remember { mutableStateOf(true) }
Column {
AnotherFunc(mutableValue = myMutableState) // Error here
Text(if (myMutableState) "Stop" else "Start")
}
}
@Composable
fun AnotherFunc(mutableValue: MutableState<Boolean>) {
}
我怎樣才能仍然使用by并且仍然能夠通過函式傳遞 MutableState?
uj5u.com熱心網友回復:
您的可組合函式應該只取一個布林值:
@Composable
fun AnotherFunc(mutableValue: Boolean) {
}
不確定為什么您的可組合函式 (AnotherFun) 需要具有可變狀態。呼叫函式(Fun)會在值變化時自動重構,觸發AnotherFun的重構。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/411497.html
標籤:
