David Maya
David Maya

Reputation: 1

It does not enter in setState() method

I have the following code in Flutter. The variable checkValue is set to false and the variable newValue is optional, so I have to check if it is null or not. Then, I change checkValue to the value of newValue inside the setState() method. My problem is that it jumps the setState() method and does not change the value.

Can anyone tell me why that happens?

CheckboxListTile(
     title: const Text('Preferences'),
     value: checkValue,
     onChanged: (newValue) {
        if (newValue != null) {
           setState() {
             checkValue = newValue;
           }
        }
     },
     controlAffinity: ListTileControlAffinity.leading,
),

I have tried to set the value with a condition like that:

CheckboxListTile(
     title: const Text('Preferences'),
     value: checkValue,
     onChanged: (newValue) {
        if (newValue != null) {
           setState() {
             checkValue = newValue ? true : false;
           }
        }
     },
     controlAffinity: ListTileControlAffinity.leading,
),

It was normal it doesn´t work xD

Upvotes: 0

Views: 38

Answers (2)

Gwhyyy
Gwhyyy

Reputation: 9206

you're not calling SetState properly: replace this:

   setState() {
         checkValue = newValue ? true : false;
       }

with this:

   setState(() {
         checkValue = newValue ? true : false;
       });

Upvotes: 0

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63799

The setState format will be

setState(() {
  ///jobs
});

For your case

setState(
  () {
    checkValue = newValue;
  },
);

Upvotes: 1

Related Questions