What is setState() and how does it work?

Beginner

Answer

setState() is the most basic way to update the UI in a StatefulWidget. It tells Flutter that the widget's state has changed and the UI needs to be rebuilt.

void _incrementCounter() {
  setState(() {
    _counter++; // Modify state inside setState
  });
}

Important: Only call setState() in StatefulWidget, and only modify state variables inside the setState() callback.