Flutter 有多种 state management approaches — 从内置的(setState、InheritedWidget)到流行的库(Provider、Riverpod、Bloc、GetX)。理解主要选项、它们的理念和权衡有助于为应用选择合适的方法。
内置方法
text
setState → local state in a StatefulWidget (simple; doesn't scale to shared state)
InheritedWidget → built-in way to share state down the tree (the foundation many
libraries build on; verbose to use directly)
Provider — 简单且推荐
text
PROVIDER → official-recommended, built on InheritedWidget; simple, widely used:
→ provide state at a point in the tree; descendants "watch"/consume it
→ ChangeNotifier holds state and notifies listeners → UI rebuilds on change
→ good for many apps; gentle learning curve
