Life Cycle of Stateless and Stateful Widgets in Flutter

Sathish Kumar
Sep 18, 2024

--

Understanding the life cycle of widgets is key to effective Flutter development. This post summarizes the life cycles of stateless and stateful widgets.

Stateless Widgets

Stateless widgets are immutable and suitable for static content. Their life cycle is simple:

  1. Creation: Instantiated via the constructor.
  2. Build Method: The build method is called to render the widget (once unless the parent rebuilds).
  3. Destruction: Automatically disposed of when removed from the widget tree

Stateful Widgets

Stateful widgets can change over time. Their life cycle includes:

  1. Creation: Widget is instantiated.
  2. State Initialization: createState creates the state object.
  3. Build Method: The state’s build method renders the widget.
  4. State Changes: setState triggers a rebuild to reflect updates.

Key Lifecycle Methods:

  • initState(): Initialization.
  • didChangeDependencies(): Called on dependency changes.
  • build(): Renders UI.
  • dispose(): Cleans up resources.

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sathish Kumar
Sathish Kumar

Written by Sathish Kumar

Hands on experience in iOS | Swift UI

No responses yet

Write a response