I have the following code in Flutter. The variable checkValue is set to false and the variable newValue is optional, so I have to check if it is null or not. Then, I change checkValue to the value of newValue inside the setState() method. My problem is that it jumps the setState() method and does not change the value.
Can anyone tell me why that happens?
CheckboxListTile(
title: const Text('Preferences'),
value: checkValue,
onChanged: (newValue) {
if (newValue != null) {
setState() {
checkValue = newValue;
}
}
},
controlAffinity: ListTileControlAffinity.leading,
),
I have tried to set the value with a condition like that:
CheckboxListTile(
title: const Text('Preferences'),
value: checkValue,
onChanged: (newValue) {
if (newValue != null) {
setState() {
checkValue = newValue ? true : false;
}
}
},
controlAffinity: ListTileControlAffinity.leading,
),
It was normal it doesn´t work xD
CodePudding user response:
The setState
format will be
setState(() {
///jobs
});
For your case
setState(
() {
checkValue = newValue;
},
);
CodePudding user response:
you're not calling SetState
properly:
replace this:
setState() {
checkValue = newValue ? true : false;
}
with this:
setState(() {
checkValue = newValue ? true : false;
});