lucy
lucy

Reputation: 21

flutter : Checkbox

Checkbox(
    value: good,
    onChanged: (val) {
      setState(() {
        good = val;
      });
    }),

There is a red line under the val good = val, what is the reason?

I didn't forget to write above bool good = false;

Upvotes: 0

Views: 164

Answers (2)

Nikhil Sharma
Nikhil Sharma

Reputation: 185

    onChanged: (val) {
      setState(() {
         good = !good; 
      });
    }

With this you can use switch good value from true to false or false to true every time you trigger this function

Upvotes: 1

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63649

onChanged provide nullable bool, define as

{required void Function(bool?)? onChanged}

you can provide false on null case like

onChanged: (val) {
  setState(() {
    good = val??false;
  });
}

Find more about null-safety

Upvotes: 2

Related Questions