Contents
CWCoding With DhrumilCW Practical Flutter and Kotlin tutorials for working mobile developers

Home › Articles

StatelessWidget vs StatefulWidget: Which One Should You Pick?

By Rahul Mehta · Published · 5 min read

Two smartphones side by side showing different app screen states for comparison.
Two smartphones side by side showing different app screen states for comparison. · Foto: rmartinr / Pixabay

Almost every Flutter screen you build starts with one decision: does this widget need to hold changing data, or can it stay fixed? StatelessWidget and StatefulWidget look like a simple either-or, but the choice affects rebuild cost, lifecycle hooks and how readable your code stays as the screen grows.

This guide compares both across the points that actually matter day to day: what triggers a rebuild, how often setState commits work, and which one keeps a widget tree easier to reason about. You will also get concrete rules for splitting stateful and stateless parts so you rarely have to guess.

The Core Difference: Immutable Description vs Mutable State

A StatelessWidget is an immutable description of part of the UI. It is constructed with final fields, its build method runs when the widget is created or when its parent rebuilds it, and it has no memory between frames. If the data it displays changes, a new widget instance arrives from above and build runs again.

A StatefulWidget is still immutable, but it creates a State object that survives rebuilds. The State holds mutable fields, exposes setState to request a rebuild, and has lifecycle methods such as initState, didChangeDependencies, didUpdateWidget and dispose. That state object outlives the widget configuration, which is what lets it hold controllers, streams and animation objects.

The practical rule: stateless widgets describe, stateful widgets coordinate. A stateless widget can still receive callbacks and trigger work; it just cannot own changing data. Pushing ownership upward often means the stateless version can be reused in more places with less risk.

Lifecycle: What Each Widget Actually Gets

StatelessWidget has one lifecycle method that matters: build. That is a feature, not a limitation. There is no initState to misuse, no dispose to forget, and no chance of keeping a stale reference after the widget leaves the tree. For pure presentation, this makes bugs far less likely than in a stateful widget that quietly holds onto an old value.

A State object goes through a longer sequence: createState, initState, didChangeDependencies, build, didUpdateWidget on configuration changes, and dispose. Most real bugs live in the gaps. Starting a network request in build, subscribing to a stream in didChangeDependencies without checking, or forgetting to cancel timers in dispose are all common and all avoidable.

If you are wiring up lifecycle correctly, the mistakes tend to be structural rather than syntactic. A quick read of common coroutine mistakes on Android shows the same pattern on the Kotlin side: the API is fine, but ownership and cancellation are where code breaks.

  • initState: one-time setup; no BuildContext-dependent work that relies on inherited widgets
  • didChangeDependencies: safe place to react to Theme or MediaQuery changes
  • didUpdateWidget: compare old and new config before restarting work
  • dispose: cancel timers, close controllers, remove listeners
0.1Stateless leaf0.4Small stateful wid0.8List item row2.5Full screen6Deep nested screen
Typical rebuild cost by widget scope (mid-range device) (milliseconds)
StatelessWidget vs StatefulWidget at a glance
AspectStatelessWidgetStatefulWidgetPractical impact
Mutable fieldsNone (final only)Held in State objectState survives rebuilds
Lifecycle methodsbuild onlyinitState, didChangeDependencies, didUpdateWidget, disposeMore hooks, more teardown to manage
Rebuild triggerParent rebuild or new instancesetState or parent rebuildsetState scopes rebuilds to the State's subtree
Typical rebuild costLow; const skips workLow if scoped, high if screen-wideKeep stateful widgets small
Best forPresentation, formatting, layoutControllers, timers, local UI stateMost widgets should be stateless
Testing effortSimple widget testsNeeds lifecycle-aware testsStateless parts test faster

Rebuild Cost: What setState Really Does

Calling setState marks the element dirty and schedules a rebuild in the next frame. The build method of that State runs again, and so do the build methods of any children whose configuration changed. Flutter does not diff widget trees; it rebuilds widgets and then compares the resulting elements and render objects. Rebuilding is cheap; layout and paint are the expensive parts.

In practice, a full screen rebuild usually costs well under a frame budget on mid-range devices. The budget at 60 fps is about 16 ms, and at 120 fps about 8 ms. A simple build method takes a fraction of a millisecond; a very deep tree with expensive layout can eat several milliseconds. That gap is why the size of the stateful widget matters much more than whether it is stateful at all.

The common mistake is making a whole page stateful when only a counter or a toggle changes. Every setState then rebuilds headers, lists and static decorations. Wrapping just the changing part in its own small StatefulWidget keeps rebuilds local and makes the code easier to test.

When StatefulWidget Is the Right Call

StatefulWidget earns its place whenever something must persist across rebuilds or has a teardown requirement. Text controllers, focus nodes, animation controllers, scroll controllers, timers and stream subscriptions all belong to a State object. Putting them in a stateless widget means recreating them on every build, which leaks memory or resets user input.

It is also the right choice for local UI state that does not belong in a shared store: whether a panel is expanded, which tab is selected, a form's validation status. Keeping that state as close as possible to the widget that uses it avoids passing callbacks through five layers. When the state needs to be shared, lift it to a parent or a state management layer instead of duplicating it.

If your stateful widget wraps platform-styled components, the styling decision comes first. A short read on Cupertino versus Material widgets will save you from mixing two design systems inside one state object.

  • You need controllers, timers or subscriptions with cleanup
  • The value changes in response to user input on this screen only
  • The widget owns an animation or a scroll position
  • You need didUpdateWidget to react to new configuration

When StatelessWidget Keeps the Code Simpler

Most widgets in a healthy Flutter app are stateless. Rows, cards, list tiles, badges and formatted text do not need memory. Making them stateless means fewer moving parts, easier widget tests, and no accidental coupling to a parent's state. It also makes const construction possible, which lets Flutter skip rebuilding identical widget subtrees entirely.

Stateless widgets are also the natural output of good decomposition. Once a stateful parent owns the data, the children can be pure functions of that data plus callbacks. That pattern scales: a screen with one stateful root and twenty stateless children is far easier to reason about than ten widgets each holding a piece of the same state.

For list-heavy screens, the same idea applies to adapter sizing. The guidance in how to size a RecyclerView adapter maps cleanly onto Flutter's ListView.builder: keep the stateful part small, let item widgets stay stateless, and measure before optimising.

Splitting State: A Practical Pattern

Start by sketching the screen and marking which values change. Anything that changes goes into one owner, usually the screen-level StatefulWidget or a state management object. Everything else becomes a stateless child that receives values and callbacks through its constructor. This gives you a clear boundary you can test.

Next, look for subtrees that rebuild too often. If a text field's setState rebuilds a long list, extract the list into a separate widget that takes a stable input. Flutter's const and RepaintBoundary help, but the structural fix is isolating state so the rebuild scope matches the change.

Finally, review teardown. Every controller created in initState needs a matching dispose. If you cannot find the dispose for a subscription, the widget is probably doing too much and should be split.

  • One stateful owner per screen or feature area
  • Stateless children with final fields and callbacks
  • Extract frequently changing subtrees into their own widgets
  • Always pair initState with dispose
  • Default to StatelessWidget; add state only where data must survive rebuilds.
  • Keep the stateful part small so setState rebuilds a narrow subtree, not the whole page.
  • Pair every controller, timer and subscription in initState with cleanup in dispose.
  • Use const and stateless children to skip unnecessary rebuild work.
Rahul Mehta · Mobile tooling writer
Specialises in setup guides, build tooling and performance checks for mobile projects. Regularly benchmarks lists, scrolling and startup behaviour to give readers concrete numbers.

Related articles

↑