状态 (State) 管理参考
总体概览 - Provider
- Riverpod
- setState
- ValueNotifier & InheritedNotifier
- InheritedWidget & InheritedModel
- June
- Redux
- Fish-Redux
- BLoC / Rx
- GetIt
- MobX
- Dart Board
- Flutter Commands
- Binder
- GetX
- states_rebuilder
- Triple Pattern (Segmented State Pattern)
- solidart
- flutter_reactive_value
- Elementary
状态管理是一个相当复杂的话题。如果你在浏览后发现一些问题并未得到解答,或者并不适用于你的具体需求场景,自信些,你的实现就是对的。
通过下面的链接了解更多的信息,其中有很多信息都是由社区(第三方)提供。
总体概览
#在选择一个具体内容前,你可以先查看以下几项。
-
状态管理的介绍。这是本篇内容的总起。(当你直接跳转到了该页面,但错过了其他页面时,可以先从这里开始阅读)
-
Flutter 实用状态管理 (Pragmatic State Management in Flutter),来自 Google I/O 2019 的介绍视频。
-
Flutter 架构实例 (Flutter Architecture Samples), Brian Egan 著。
Provider
#Riverpod
#Riverpod 是另一个不错的选择,它类似于 Provider,并且是编译安全和可测试的。 Riverpod 不依赖于 Flutter SDK。
-
Riverpod 项目主页
setState
#The low-level approach to use for widget-specific, ephemeral state.
-
为你的 Flutter 应用添加交互 (Adding interactivity to your Flutter app),一篇 Flutter 的教程。
-
Flutter 中的基础状态管理 (Basic state management in Google Flutter), Agung Surya 著。
ValueNotifier & InheritedNotifier
#An approach using only Flutter provided tooling to update state and notify the UI of changes.
- State Management using ValueNotifier and InheritedNotifier, by Tadas Petra
InheritedWidget & InheritedModel
#Widget tree 中不同层级间的 widget 通信的基础方法。这是诸如 provider
等众多方法的底层实现。
以下讲师指导的视频 workshop 介绍了如何使用 InheritedWidget
:
其他有用的文档包括:
-
使用 InheritedWidgets 管理 Flutter 应用状态 (Managing Flutter Application State With InheritedWidgets), Hans Muller 著。
-
继承 Widgets (Inheriting Widgets), Mehmet Fidanboyly 著。
-
高效地使用 Flutter 继承 Widgets (Using Flutter Inherited Widgets Effectively),Eric Windmill 著。
-
Widget - State - Context - InheritedWidget,Didier Bolelens 著。
June
#一个轻量级的现代状态管理库,专注于提供与 Flutter 内置状态管理类似的模式。
Redux
#前端开发者较为熟悉的状态容器实现。
-
使用 Redux 在 Flutter 中管理动画 (Animation Management with Redux and Flutter),来自 DartConf 2018 的视频, 以及 Medium 的配套文章 (Accompanying article on Medium)。
-
Dart 与 Flutter 中的 Redux 中间件 Saga,Bilal Uslu 著
-
Flutter 中的 Redux 介绍, Xavi Rigau 著。
-
Flutter + Redux—构建一个购物列表 APP (Flutter + Redux—How to make a shopping list app),发布于 Hackernoon,Paulina Szklarska 著。
-
用 Flutter Redux 构建一个任务应用 (CRUD) —第一部分 (Building a TODO application (CRUD) in Flutter with Redux—Part 1),由 Tensor Programming 制作的视频。
-
Flutter Redux Thunk 示例 (Flutter Redux Thunk, an example),Jack Wong 著。
-
使用 Redux 构建(大型)Flutter 应用 (Building a (large) Flutter app with Redux),Hillel Coren 著。
-
异步 Redux—没有模板的 Redux,允许同步和异步 reducers (Async Redux–Redux without boilerplate. Allows for both sync and async reducers),Marcelo Glasberg 著。
-
当 Flutter 遇见 Redux:以 Redux 的风格管理 Flutter 应用的状态 (Flutter meets Redux: The Redux way of managing Flutter applications state),Amir Ghezelbash 著。
-
使用 Redux 和 redux_epics 更好的管理 Flutter 代码,Nihad Delic 著。
-
使用 Redux 更好地组织 Flutter 应用的代码以管理项目目标 (Redux and epics for better-organized code in Flutter apps),Nihad Delic 著。
Fish-Redux
#Fish Redux 是一个基于 Redux 状态管理的组合式 Flutter 应用框架,适用于构建中型和大型应用。
-
由阿里巴巴开发的 Fish-Redux-Library package
-
Fish-Redux-Source,工程代码
-
Flutter-Movie 展示如何使用 Fish Redux 的简单示例应用,包含 30 多个页面、graphql、支付 api 和媒体播放器等。
BLoC / Rx
#基于流/观察者模式的系列。
-
使用 BLoC 模式构建你的 Flutter 项目 (Architect your Flutter project using BLoC pattern), Sagar Suri 著。
-
BLoC 库 (BLoC Library),Felix Angelov 著。
-
响应式编程 - 流 - BLoC - 用例 (Reactive Programming - Streams - BLoC - Practical Use Cases), Didier Boelens 著。
GetIt
#A service locator based state management approach that
doesn't need a BuildContext
.
- GetIt package, the service locator. It can also be used together with BloCs.
- GetIt Mixin package, a mixin that completes
GetIt
to a full state management solution. - GetIt Hooks package, same as the mixin in
case you already use
flutter_hooks
. - Flutter state management for minimalists, by Suragch
MobX
#一个基于观察及响应的状态管理常用库。
Dart Board
#A modular feature management framework for Flutter. Dart Board is designed to help encapsulate and isolate features, including examples/frameworks, small kernel, and many ready-to-use decoupled features such as debugging, logging, auth, redux, locator, particle system and more.
- Dart Board Homepage + Demos
- Dart Board on pub.dev
- dart_board on GitHub
- Getting started with Dart Board
Flutter Commands
#基于 ValueNotifiers
的命令式的状态管理,能与 GetIt 完美结合使用,也可以与 Provider
或者其他 locators 配合使用。
- Flutter Command package
- RxCommand package,
Stream
based implementation.
Binder
#一个使用 InheritedWidget
作为核心实现的状态管理库。受到 recoil 的启发,该库提供了分治的解决方式。
-
Binder snippets 是一个 vscode 插件,能够将代码拆分以获得更高的生产力
GetX
#一个简单的响应式状态管理解决方案。
- GetX package
- GetX Flutter Firebase Auth Example, by Jeff McMorris
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:
- Triple documentation
- Flutter Triple package
- Triple Pattern: A new pattern for state management in Flutter (blog post written in Portuguese but can be auto-translated)
- VIDEO: Flutter Triple Pattern by Kevlin Ossada (recorded in English)
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.
flutter_reactive_value
source and documentation
Elementary
#Elementary is a simple and reliable way to build applications with MVVM in Flutter. It offers a pure Flutter experience with clear code separation by responsibilities, efficient rebuilds, easy testability, and enhancing team productivity.
除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2024-09-02。 查看文档源码 或者 为本页面内容提出建议。