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) {...}abbic
02/20/2023, 10:29 AMabbic
02/20/2023, 10:32 AMabbic
02/20/2023, 10:33 AMvm.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 AMabbic
02/20/2023, 10:38 AMabbic
02/20/2023, 10:38 AMisXEqualsToZeroabbic
02/20/2023, 10:39 AMval isXEqualToZero = a.x == 0abbic
02/20/2023, 10:39 AMAmrJyniat
02/20/2023, 10:43 AMbrandonmcansh
02/20/2023, 1:14 PMbrandonmcansh
02/20/2023, 1:15 PMabbic
02/20/2023, 1:16 PMabbic
02/20/2023, 1:18 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.abbic
02/20/2023, 3:18 PMAmrJyniat
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