setState isn't referenced flutter 3.0

I am working on a school project, since we updated the flutter version from 2.X to 3.0.1 we face this problem : setState isn’t referenced. We do not understand as we are using this in a statefulWidget. We went to the internet to show what the problem is but we cannot find a way to make it work because most of the time it was because people where using a stateless widget which is not our case.

Start of file :

``` class PageProfilAmi extends StatefulWidget {
  final User user;
  final String? idRelation;

  const PageProfilAmi({Key? key, required this.user, this.idRelation})
      : super(key: key);

  _PageProfilAmiState createState() => _PageProfilAmiState();

class _PageProfilAmiState extends State<PageProfilAmi> {
  IconData _icon = Icons.add;

  void initState() {
  } ``` 

Where we have the issue :

``` IconButton(
            onPressed: () {
                setState() {
                              _icon = Icons.delete;

                icon: Icon(
                           color: CustomColors.MAIN_PURPLE,
                           size: 20,
        ) ```

>Solution :

enter code hereHere issue is in syntax of setState

setState(() { 

// code 


Leave a Reply