0

Why Flutter defines that we should call:

setState(() { _counter++});

instead of:

_counter++;
setState(() {});

As far as I can see in setState() code, it doesn't use anything that's passed as a parameter anyway.

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173

1 Answers1

3

The end result in release mode is the same.

But in debug you get an assert for free that checks that the callback inside setState() does not return a Future and it returns immediately.

But if you are sure the callback is synchronous, the result in debug is the same.

chemamolins
  • 19,400
  • 5
  • 54
  • 47
  • Ok, but I still don't get why to force people to pass something to `setState()`... If it was called `invalidateState()` and before calling it we set all the necessary variables, call all the Futures we want, won't it be the same? – Michel Feinstein Nov 12 '18 at 23:13
  • Well, they don't force you. It is a best practice to avoid te asynchronous problems. But since you can set it outside and it works, you can do it. – chemamolins Nov 12 '18 at 23:18