skip to Main Content

bool isChecked = false;

enter image description here

class _TasksTileState extends State<TasksTile> {   
 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;
    });
   },
  );
 }
}

2

Answers


  1. 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;

    Login or Signup to reply.
  2. 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!;
                });
              },
            );
          }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search