AmrJyniat
02/20/2023, 10:13 AMdata class A(val x: Int)
Inside the Composable:
@Composable
fun Test(vm: ViewModel){
val a by vm.a.collectAsState()// I want x attribute not a object
}
Is there a way to accomplish that?abbic
02/20/2023, 10:22 AMa.x
where you wish to access the attributeAmrJyniat
02/20/2023, 10:28 AMx
to produce another value like:
val a vm.a.collectAsState()
val isXEqualsToZero by remember {
derivedStateOf { a.x == 0 }
}
It's more convenient to get rid of a
and use x
directlyabbic
02/20/2023, 10:29 AMval x = a.x
first, or wrap you code in with (a) {...}
vm.x.collectAsState()
AmrJyniat
02/20/2023, 10:35 AMderivedStateOf
directly without creating another state for it?
Idk if snapShotFlow{}
can help in such casesabbic
02/20/2023, 10:36 AMAmrJyniat
02/20/2023, 10:37 AMx
in viewModelabbic
02/20/2023, 10:37 AMisXEqualsToZero
val isXEqualToZero = a.x == 0
AmrJyniat
02/20/2023, 10:43 AMbrandonmcansh
02/20/2023, 1:14 PMabbic
02/20/2023, 1:16 PMbrandonmcansh
02/20/2023, 3:12 PMabbic
02/20/2023, 3:17 PMderivedStateOf
is only necessary when performance improvements are needed. as is repeated throughout compose documentation when performance is involved, optimisations should not be undertaken without there being evidence of an actual performance hit. compose is performant by default in the vast majority of cases, so its not really correct to say that derivedStateOf
is the way to go.AmrJyniat
02/20/2023, 3:20 PMX
here is a state that changed by clicking a buttonabbic
02/20/2023, 3:20 PMbrandonmcansh
02/20/2023, 3:28 PM