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 do I have to do a hot reload to change the icon?

I’m trying to save the state of an icon, it should change on click, but it only changes on hot reload, what’s my mistake?

IconButton –

IconButton(onPressed: () {
  if (_favorite == Icons.favorite_border) {
    _favorite = Icons.favorite;
   print(1111);
  } else {
    _favorite = Icons.favorite_border;
    print(2222);
  }
}, icon: Icon(_favorite, color: configColors.orange, size: 10,)),

setState –

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

IconData _favorite = Icons.favorite_border;
  void initState() {
    _favorite = Icons.favorite_border;
    super.initState();

  }

>Solution :

Inside onPressed call the setState to update the UI for StatefulWidget

IconButton(onPressed: () {
  if (_favorite == Icons.favorite_border) {
    _favorite = Icons.favorite;
   print(1111);
  } else {
    _favorite = Icons.favorite_border;
    print(2222);
  }
  setState((){}); //here
}, icon: Icon(_favorite, color: configColors.orange, size: 10,)),
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