How does Flutter treat state
UI = f(state)
Flutter inserts information to display from the state, so whenever state is changed widgets affected by this state have to be rebuilt.
Source: https://flutter.dev/docs/development/data-and-backend/state-mgmt/declarative
State in Flutter is stored as fields of State
inheritants. Stateful widgets maintain state that might change during the lifetime of the widget. Implementing a stateful widget requires at least two classes: 1) a StatefulWidget class that creates an instance of 2) a State class.
State is detached from a widget
The StatefulWidget class is, itself, immutable and can be thrown away and regenerated, but the State class persists over the lifetime of the widget.
setState(() {})
Change the state with Flutter takes a function as a parameter that is resposible for state update. Passed in lambda should just update the fields, and will do so in an asynchronous manner.
Key
s makes things clear for Flutter
Key
are important for Flutter to recognize which state should be attached to which widget after a rebuild. Each Widget
inheritant can pass a Key
instance to the parent class:
tip
There's a great Emily Fortuna's explaination of Key
s inner workings here.