skip to Main Content

Why cannot I use setState in any of my buttons in flutter?
This is my code of the button:

TextButton(
                              style: TextButton.styleFrom(
                                fixedSize: Size(
                                    MediaQuery.of(context).size.width / 2, 100),
                              ),
                              onPressed: setState(() {
                                logPage = false;
                              }),
                              child: const Text(
                                "Sign Up",
                                style: TextStyle(fontSize: 20),
                              ),
                            )

but the setState command is underlined and Android Studio says:
This expression has a type of 'void' so its value can't be used.

I would like to change the variable and rebuild my app page to get it updated and it should show changes.

3

Answers


  1. Try this:

    onPressed: () => setState(() {
      logPage = false;
    }),
    
    Login or Signup to reply.
  2. You are invoking setState() in the definition of your onPressed:. You need to provide a function to be run at a future time, as in a VoidCallback. It’d look something like:

    onPressed: () { setState(() { logPage = false; }),
    

    Notice that the onPressed is now a function, not the result of calling a function.

    Login or Signup to reply.
  3. Try to clear build cache :

    flutter clean
    

    an rebuild again

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search