Interview frequency: …
Short answer
setState tells the framework that the State changed and requests a rebuild of the StatefulElement and its children; put the state at the nearest logical owner.
Explanation
Modify the state inside the callback and then let Flutter rebuild the affected subtree. Do not call setState after dispose, and do not put state belonging to a small element at the top of the screen without a reason.
Code example
setState(() {
isExpanded = !isExpanded;
});Common mistakes
- Changing a field outside setState and expecting the UI to update.
- Calling setState after an await without checking mounted.