I am trying to access the dao instance of my room database which is being used to store names(This code I wrote just to get familiar to jetpack compose).
I tried to access my dao instance inside my composable function but is giving me this error -> Composable calls are not allowed inside the calculation parameter of inline fun remember(calculation: () -> TypeVariable(T)): TypeVariable(T)
My Code
@Composable
fun HomeScreen(navController: NavController){
var dao by remember{
mutableStateOf(NameDatabase.getInstance(LocalContext.current).getDao())
}
var scope = rememberCoroutineScope()
var username by rememberSaveable{ mutableStateOf("") }
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
){
OutlinedTextField(value = username, onValueChange = {username=it})
Spacer(modifier = Modifier.height(10.dp))
Button(onClick = {
navController.navigate("showScreen/$username")
}) {
Text("Submit")
}
}
}
I am getting the error on this line
mutableStateOf(NameDatabase.getInstance(LocalContext.current).getDao())
Specifically LocalContext.current
is giving this error.
PS: I have resolved this error but want to know what is the meaning of this error any why I can't get the dao instance inside remember.
CodePudding user response:
You need to read the value outside of the function:
val context = LocalContext.current
var dao by remember {
mutableStateOf(NameDatabase.getInstance(context).getDao())
}