Issue
I am making a form with switches in Flutter and I would like to save the value of it, but Flutter doesn’t want a value bool? so I don’t know how to get my value.
Here is my code :
Switch(
value: _editedProduct.myswitchvalue, //line that causes problem
onChanged: (bool val) =>
setState(() {
_editedProduct = _editedProduct.copyWith(myswitchvalue: val);
},
),
),
And here’s the message of my error
The argument type 'bool?' can't be assigned to the parameter type 'bool'.dart(argument_type_not_assignable)
FormProductModel _editedProduct
Thank you for reading me
Solution
myswitchvalue is of type bool? which means it can either be null or boolean. If you are sure it is not null you can add ! like that:
Switch(
value: _editedProduct.myswitchvalue!, // watch the !
onChanged: (bool val) =>
setState(() {
_editedProduct = _editedProduct.copyWith(myswitchvalue: val);
},
),
),
read more about null safety in dart here: https://dart.dev/null-safety
Answered By – Yair Chen
Answer Checked By – Marie Seifert (FlutterFixes Admin)