Flutter's State Lifecycle: From Creation to Disposal
A Flutter State object outlives its widget configuration. The framework manages its journey from creation (initState) to permanent removal (dispose), calling methods like build along the way. This governs all StatefulWidgets.
Why it exists
Flutter's declarative UI means widgets are frequently rebuilt. To preserve information across these ephemeral rebuilds, the framework separates the configuration (the Widget) from the long-lived, mutable state (the State object). The state lifecycle provides predictable hooks for managing this persistent state.
The mental model
A State object is a stage actor, and the Widget is their script for a scene. The actor (State) is hired once (initState) and stays on set. They may get revised scripts (didUpdateWidget) and perform the scene many times (build). They might leave the stage briefly (deactivate) and return, but only when their contract ends (dispose) do they pack up and go home for good. The State is the persistent entity; the Widget is just its current configuration.
How it works
The framework creates a State object via StatefulWidget.createState. The object is now mounted. The framework then calls initState for one-time setup. After that, didChangeDependencies runs. From this point, build can be called many times to render the UI. If the parent provides a new widget configuration, didUpdateWidget is called, followed by another build. If the widget is removed from the tree, deactivate is called. If it's not reinserted into the tree by the end of the animation frame, dispose is called, the object is unmounted, and it will never be used again.
When to use it
You use these lifecycle methods in every StatefulWidget. Use initState to subscribe to streams or initialize controllers. Use didUpdateWidget to react to changes in the widget's constructor parameters. Use build to describe your UI based on the current state. Crucially, use dispose to cancel subscriptions and release resources to prevent memory leaks.
When not to use it
Don't perform expensive work in the State constructor; initState is the correct place, as context is available there. Don't call setState within didUpdateWidget, as a build is already scheduled to run after it. Do not put final resource cleanup in deactivate; since the state object might be re-inserted into the tree, this can cause errors. Cleanup belongs in dispose.
One canonical example
Initializing an AnimationController in initState and releasing it in dispose is a classic use case. This ensures the controller exists for the entire life of the State object but is properly cleaned up when the State is permanently destroyed, preventing memory leaks.
Interview question
Which lifecycle method is the definitive place to release resources like an AnimationController when a StatefulWidget is permanently removed?
- a.The didUpdateWidget method, to react to the widget's configuration changes.
- b.The State object's constructor, as it's the first point of creation.
- c.The dispose method, as it's called when the State object is permanently destroyed.Correct
- d.The deactivate method, as it signifies the widget's removal from the tree.
Why? this is the answer
The dispose method is explicitly for final resource cleanup when the State object is permanently removed and will never be used again. Deactivate, while indicating removal, is for temporary situations where the State object might be re-inserted into the tree.
Just read this? Test yourself on what you have been reading.
Read the original → api.flutter.dev
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.
See open roles