Blog

[Flutter] State

Published 1 min read

State

Data that affects the UI so it changes

Data

App-level data
The data we usually think of
ex) data used for user authentication or when fetching info stored on a server

Widget-level data
Every action that changes the app's state
ex) actions related to selecting a Radio button or text being typed into a text field

Stateless widget

A widget whose State doesn't change
It doesn't change unless the widget itself is rebuilt. → A new State can only be applied through a rebuild.

Stateful widget

A widget whose State changes.
A Stateful Widget has another class called State inside it.
In a Stateful widget, the State class has the Build method.
So the State class is in charge of rebuild and Rendering.

A Stateful widget is rebuilt in these cases :

  1. When new data is passed through the Child widget's constructor
  2. When the Internal state changes

Say there's a Stateful MyApp widget.
When setState() or the like is called,
a new MyApp Stateful widget is created and it rebuilds.
The changed State is stored in the MyAppState object, and
the element is connected to the new MyApp Stateful widget.

The reason the MyAppState object isn't rebuilt here is to reduce cost.
Rebuilding the object too is expensive, so only the relatively cheap Stateful widgit is rebuilt.

Widget lifecycle

[flutter] Flutter StatefulWidget lifecycle

Comments

Leaving a comment stores your nickname (or GitHub profile info). Privacy policy

GitHub