seniorFlutter

Explain the internal mechanism of `setState` (marking elements dirty).

Updated Apr 28, 2026

Short answer

setState is simply a framework trigger that marks the current Element as 'dirty' and schedules it for a rebuild.

Deep explanation

Internally, setState(fn) executes your provided function, then calls element.markNeedsBuild(). This adds the Element to a global _dirtyElements list. During the next frame pipeline (driven by the Engine's Vsync signal), the framework iterates through dirty elements, calling build() to generate new widget configurations, and reconciling the tree.

Unlock with a Pro subscription to view this section.

View pricing

Real-world example

No real-world example available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Common mistakes

No common mistakes listed yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Follow-up questions

No follow-up questions available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

More Flutter interview questions

View all →