状态管理 是指应用程序如何管理和更新其**数据(状态)**并在 UI 中反映这些变化。随着应用程序的增长,良好的状态管理变得很重要,Flutter 提供了内置机制(setState)和许多库(Provider、Riverpod、Bloc)来实现它。
什么是状态
text
STATE = data that can change and affects the UI:
→ UI state: is a checkbox checked? what's in a text field? is a menu open?
→ app state: the logged-in user, items in a cart, fetched data
→ When state changes, the UI must UPDATE to reflect it.
本地状态:setState
dart
// for state local to a single widget → StatefulWidget + setState (built-in, simple)
setState(() {
count++; // change state → triggers a rebuild → UI updates
});
// → good for simple, LOCAL state within one widget
