The anonymous function inside setState is actually a bad design but it since it is the norm now, it passes off for a great design.

Java developers who have used java.util.Observable will easily realize that setState is doing two things at once - setChanged(true) + notifyObservers().

There is absolutely no reason to make all modifications to the state in a function passed to setState or even invoke setState upon every modification. You need to invoke setState only if you want the changes to be reflected on the UI. For example, you can keep making changes and finally invoke setState.

Of course, in real life, you can’t do that because other people will not understand the logic and will just follow the _incrementCounter(setState() {

  _counter = _counter + 1;

GitHub-flavored Markdown & a sane subset of HTML is supported.