Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why setState() method not working despite making related variable nullable

class _InputPageState extends State<InputPage>{

  Gender? selectedGender;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: mainColor,
      appBar: AppBar(
        title: Text(
          'BMI Calculator'
        ),
        backgroundColor: Colors.transparent,
        elevation: 0,
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: GestureDetector(
                    child: ReusableCard(
                        colour: selectedGender == Gender.male ? activeCardColor : inactiveCardColor,
                      cardChild: IconContent(
                        icon: FontAwesomeIcons.mars,
                        label: 'MALE',
                      ),
                    ),
                    onTap: (){
                      setState(() {
                        selectedGender == Gender.male;
                      });
                    },
                  ),
                ),`

`

setState() method isn’t updating the card color despite setting Gender? selectedGender to nullable, why is this happening?

I have tried changing selectedGender to late yet it gives the LateInitializationError, I wanted the colour: selectedGender == Gender.male ? activeCardColor : inactiveCardColor to set color to inactiveCardColor (this is working fine) and then setState() method should change it to active one upon Tap (that’s not working).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Please change selectedGender == Gender.male to selectedGender = Gender.maleinside the setstate

setState(() {
   selectedGender = Gender.male;
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading