Why Are StatefulWidget and State Separate Classes?

Technology CommunityCategory: FlutterWhy Are StatefulWidget and State Separate Classes?
VietMX Staff asked 3 years ago
  • In one word: performance.
  • State objects are long lived, but StatefulWidgets (and all Widget subclasses) are thrown away and rebuilt whenever configuration changes. It’s very inexpensive i.e. cheap for Flutter to rebuild a mutable widget.
  • As State isn’t blown away on every rebuild, it avoids expensive computations, and gets at the states propertygetterssetters, etc. every time something is rebuilt frame by frame.
  • Important is that this is what allows Flutter animations to exist. As State isn’t thrown away, it can constantly be rebuilding it’s Widget in response to data changes, and when required, if any.