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
Try this:
You are invoking
setState()
in the definition of youronPressed:
. You need to provide a function to be run at a future time, as in a VoidCallback. It’d look something like:Notice that the onPressed is now a function, not the result of calling a function.
Try to clear build cache :
an rebuild again