Abhimanyu
05/28/2024, 3:00 PMmutableState
.
From the docs (https://developer.android.com/develop/ui/compose/state#state-in-composables),
I see that I can create a mutableState
using either of these syntax.
var id: Int? by remember {
mutableStateOf(null)
}
val setId = { updatedId: Int? ->
id = updatedId
}
OR
val (id: Int?, setId) = remember {
mutableStateOf(null)
}
I want to pass the method setId
to function that has the method signature as setId: (Int?) -> Unit
.
The first syntax where the setId
was manually created by me has the required method signature as expected, but the setter method in the second syntax has the method signature as (Nothing?) → Unit
.
So, I am getting a data type mismatch error.
Can anyone please share why this happens, and what is the best way to do this? thank you colorStylianos Gakis
05/28/2024, 3:12 PMmutableStateOf<Int?>(null)
. Or even use mutableIntStateOf instead.
With that said, I'd personally suggest to never use the destructuring syntax anyway, it's inferior to the delegate option for various reasons.vide
05/28/2024, 3:13 PMmutableStateOf
. For example
val id: Int? = mutableStateOf(null)
won't work either.Abhimanyu
05/28/2024, 3:22 PMWith that said, I'd personally suggest to never use the destructuring syntax anyway, it's inferior to the delegate option for various reasons.
So, the suggestion is to always use the first syntax and manually create the setters?
vide
05/28/2024, 3:33 PMStylianos Gakis
05/28/2024, 3:39 PM() -> State
, you've read the state at declaration.vide
05/29/2024, 3:33 PMThese declarations are equivalent
Abhimanyu
05/29/2024, 3:34 PMStylianos Gakis
05/29/2024, 7:30 PM