状态 (State) 管理参考

状态管理是一个相当复杂的话题。如果你在浏览后发现一些问题并未得到解答,或者并不适用于你的具体需求场景,自信些,你的实现就是对的。

通过下面的链接了解更多的信息,其中有很多信息都是由社区(第三方)提供。

总体概览

在选择一个具体内容前,你可以先查看以下几项。

Provider

Riverpod

Riverpod 是另一个不错的选择,它类似于 Provider,并且是编译安全和可测试的。 Riverpod 不依赖于 Flutter SDK。

setState

The low-level approach to use for widget-specific, ephemeral state.

InheritedWidget & InheritedModel

Widget tree 中不同层级间的 widget 通信的基础方法。这是诸如 provider 等众多方法的底层实现。

以下讲师指导的视频 workshop 介绍了如何使用 InheritedWidget

其他有用的文档包括:

June

一个轻量级的现代状态管理库,专注于提供与 Flutter 内置状态管理类似的模式。

Redux

前端开发者较为熟悉的状态容器实现。

Fish-Redux

Fish Redux 是一个基于 Redux 状态管理的组合式 Flutter 应用框架,适用于构建中型和大型应用。

BLoC / Rx

基于流/观察者模式的系列。

GetIt

A service locator based state management approach that doesn’t need a BuildContext.

MobX

一个基于观察及响应的状态管理常用库。

Flutter Commands

基于 ValueNotifiers 的命令式的状态管理,能与 GetIt 完美结合使用,也可以与 Provider 或者其他 locators 配合使用。

Binder

一个使用 InheritedWidget 作为核心实现的状态管理库。受到 recoil 的启发,该库提供了分治的解决方式。

GetX

一个简单的响应式状态管理解决方案。

states_rebuilder

一种将状态管理与依赖注入解决方案和集成路由器相结合的方法。更多信息,请参阅以下信息:

Triple Pattern (Segmented State Pattern)

Triple is a pattern for state management that uses Streams or ValueNotifier. This mechanism (nicknamed triple because the stream always uses three values: Error, Loading, and State), is based on the Segmented State pattern.

For more information, refer to the following resources:

solidart

A simple but powerful state management solution inspired by SolidJS.

flutter_reactive_value

The flutter_reactive_value library might offer the least complex solution for state management in Flutter. It might help Flutter newcomers add reactivity to their UI, without the complexity of the mechanisms described before. The flutter_reactive_value library defines the reactiveValue(BuildContext) extension method on ValueNotifier. This extension allows a Widget to fetch the current value of the ValueNotifier and subscribe the Widget to changes in the value of the ValueNotifier. If the value of the ValueNotifier changes, Widget rebuilds.