How can I archive tristate
for checkbox with nullsafety.... i have to pass null into that value but nullsafety has to perform null check on a variable, this is just contradicting .....
Putting null check as usual:
bool? parentvalue;
void update() {
parentvalue = null;
}
Checkbox(
....
value: parentvalue!,
onChanged: update(),
....
),
ERROR: Null check operator used on a null value
if I remove null check, code cannot compile at all
bool parentvalue;
void update() {
parentvalue = null;
}
Checkbox(
....
value: parentvalue,
onChanged: update(),
....
),
ERROR: A value of type 'Null' can't be assigned to a variable of type 'bool'.
CodePudding user response:
the value of Checkbox must be nut null
if you want to Checkbox be unChecked when value is null you can use this instead of null check
value: parentvalue??false
CodePudding user response:
You need to add tristate: true
in order to accept null. Also, the update()
method needed to be changed because it provides nullable bool
on callback.
Checkbox(
tristate: true,
value: parentvalue,
onChanged: update,
// onChanged:(value) { update(); },
),
And update method will be
void update(value) {
parentvalue = value;
setState(() {});
}
More about Checkbox
on flutter.dev.