Home > front end >  setState isn't referenced flutter 3.0
setState isn't referenced flutter 3.0

Time:06-17

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);

  @override
  _PageProfilAmiState createState() => _PageProfilAmiState();
}

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

  @override
  void initState() {
    super.initState();
  } ``` 

Where we have the issue :

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

                AuthController.deleteAmi(
                           widget.idRelation.toString());
                          },
                icon: Icon(
                           _icon,
                           color: CustomColors.MAIN_PURPLE,
                           size: 20,
                          ),
        ) ```

CodePudding user response:

enter code hereHere issue is in syntax of setState

setState(() { 

// code 

 });

CodePudding user response:

You have setState written wrong. In a statefull widget setState is called like this:

setState(() {
    //do something
  });

Basically you are missing the outside parentheses '(' ')'

  • Related