Note: I have come up with a solution, but it feels dirty. So..
I have this controller file
class SignupController extends GetxController {
static SignupController get instance => Get.find();
final checkbox = true.obs;
// this function returns the value correctly, either true or false
testOne (){
print(checkbox.value); // this value changes
}
// this function always returns 'TRUE' regardless of the changes
Future<void> signup() async{
try{
if(!checkbox.value) {
mySnackBar(
title: 'Accept',
message: 'To create an account please accept'
);
print(checkbox.value); // This value is always 'true'...why?
return;
}
}
I call the function like this
onPressed: () => SignupController().signup();
// I change value like this
Checkbox(
value: privacyPolicy.value,
onChanged: (value) {
controller.privacyPolicy.value = !controller.privacyPolicy.value;
}
)
NOTE: the observable state changes on the screen. check box tick works fine, its when I try to get the current value that I get this problem
My workaround was to bypass the controller class and pass the value of the checkbox manually… i.e.
Future<void> signup(checkbox) async{
try{
if(!checkbox.value) {
mySnackBar(
title: 'Accept',
message: 'To create an account please accept'
);
print(checkbox.value); // This value now returns correctly
return;
}
//// and defice checkbox here
Widget build(BuildContext context) {
final controller = Get.put(SignupController());
Rx<bool> checkbox = true.obs;
But I want to have my controllers in my controller file
Adam Online is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.