bool isChecked = false;
A value of type 'bool?' can't be assigned to a variable of type 'bool'.
class _TasksTileState extends State { bool isChecked = false; @override Widget build(BuildContext context) { print('$isChecked is the value of isChecked'); return CheckboxListTile( title: Text('this is the Task 1.',style: TextStyle( decoration: isChecked? TextDecoration.lineThrough: null, ),), value: isChecked, onChanged: (newValue) { setState(() { print('$newValue is the value of newValue'); isChecked = newValue; }); }, ); } }
CodePudding user response:
problem is onChanged: (newValue)
, newValue is nullable, and your isChecked
variable is not. you cannot assign nullable variable to non null. Either make isChecked nullable (bool? isChecked = false
) or supply default value when assigning: isChecked = newValue ?? false;
CodePudding user response:
Here is correct way to use bool all you need is to add null operator
bool isChecked = false;
@override
Widget build(BuildContext context) {
print('$isChecked is the value of isChecked');
return CheckboxListTile(
title: Text(
'this is the Task 1.',
style: TextStyle(
decoration: isChecked ? TextDecoration.lineThrough : null,
),
),
value: isChecked,
onChanged: (newValue) {
setState(() {
print('$newValue! is the value of newValue');
isChecked = newValue!;
});
},
);
}