状态 (State) 管理参考
状态管理是一个相当复杂的话题。如果您在浏览后发现一些问题并未得到解答,或者并不适用于您的具体需求场景,自信些,您的实现就是对的。
State management is a complex topic. If you feel that some of your questions haven’t been answered, or that the approach described on these pages is not viable for your use cases, you are probably right.
通过下面的链接了解更多的信息,其中有很多信息都是由社区(第三方)提供。
Learn more at the following links, many of which have been contributed by the Flutter community:
总体概览
General overview
在选择一个具体内容前,您可以先查看以下几项。
Things to review before selecting an approach.
-
状态管理的介绍。这是本篇内容的总起。(当您直接跳转到了该页面,但错过了其他页面时,可以先从这里开始阅读)
Introduction to state management, which is the beginning of this very section (for those of you who arrived directly to this Options page and missed the previous pages)
-
Flutter 实用状态管理 (Pragmatic State Management in Flutter),来自 Google I/O 2019 的介绍视频。
Pragmatic State Management in Flutter, a video from Google I/O 2019
-
Flutter 架构实例 (Flutter Architecture Samples), Brian Egan 著。
Flutter Architecture Samples, by Brian Egan
Provider
推荐的管理方式。
A recommended approach.
-
简易的应用状态管理 (Simple app state management),这是本节内容的上篇。
Simple app state management, the previous page in this section
-
你可能并不需要 Redux:Flutter 专版 (You might not need Redux: The Flutter edition)
You might not need Redux: The Flutter edition, by Ryan Edge
-
学习并理解 Flutter Providers (Making sense of all those Flutter Providers)
setState
适用于较小规模 widget 的暂时性状态的基础管理方法。
The low-level approach to use for widget-specific, ephemeral state.
-
为你的 Flutter 应用添加交互 (Adding interactivity to your Flutter app),一篇 Flutter 的教程。
Adding interactivity to your Flutter app, a Flutter tutorial
-
Flutter 中的基础状态管理 (Basic state management in Google Flutter),Agung Surya 著。
Basic state management in Google Flutter, by Agung Surya
InheritedWidget & InheritedModel
Widget tree 中不同层级间的 widget 通信的基础方法。这是诸如 provider
等众多方法的底层实现。
The low-level approach used to communicate between ancestors and children
in the widget tree. This is what provider
and many other approaches
use under the hood.
-
使用 InheritedWidgets 管理 Flutter 应用状态 (Managing Flutter Application State With InheritedWidgets), Hans Muller 著。
Managing Flutter Application State With InheritedWidgets, by Hans Muller
-
继承 Widgets (Inheriting Widgets), Mehmet Fidanboyly 著。
Inheriting Widgets, by Mehmet Fidanboylu
-
高效地使用 Flutter 继承 Widgets (Using Flutter Inherited Widgets Effectively),Eric Windmill 著。
Using Flutter Inherited Widgets Effectively, by Eric Windmill
-
Widget - State - Context - InheritedWidget,Didier Bolelens 著。
Widget - State - Context - InheritedWidget, by Didier Bolelens
Redux
前端开发者较为熟悉的状态容器实现。
A state container approach familiar to many web developers.
-
使用 Redux 在 Flutter 中管理动画 (Animation Management with Redux and Flutter),来自 DartConf 2018 的视频, 以及 Medium 的配套文章 (Accompanying article on Medium)。
Animation Management with Redux and Flutter, a video from DartConf 2018 Accompanying article on Medium
-
[Dart 与 Flutter 中的 Redux 中间件 Saga][],Bilal Uslu 著
Redux Saga Middleware Dart and Flutter, by Bilal Uslu
-
Flutter 中的 Redux 介绍, Xavi Rigau 著。
Introduction to Redux in Flutter, by Xavi Rigau
-
Flutter + Redux—构建一个购物列表 APP (Flutter + Redux—How to make a shopping list app),发布于 Hackernoon,Paulina Szklarska 著。
Flutter + Redux—How to make a shopping list app, by Paulina Szklarska on Hackernoon
-
用 Flutter Redux 构建一个任务应用 (CRUD) —第一部分 (Building a TODO application (CRUD) in Flutter with Redux—Part 1),由 Tensor Programming 制作的视频。
Building a TODO application (CRUD) in Flutter with Redux—Part 1, a video by Tensor Programming
-
Flutter Redux Thunk 示例 (Flutter Redux Thunk, an example),Jack Wong 著。
Flutter Redux Thunk, an example, by Jack Wong
-
使用 Redux 构建(大型)Flutter 应用 (Building a (large) Flutter app with Redux),Hillel Coren 著。
Building a (large) Flutter app with Redux, by Hillel Coren
-
Fish-Redux–An assembled flutter application framework based on Redux, by Alibaba
-
异步 Redux—没有模板的 Redux,允许同步和异步 reducers (Async Redux–Redux without boilerplate. Allows for both sync and async reducers),Marcelo Glasberg 著。
Async Redux–Redux without boilerplate. Allows for both sync and async reducers, by Marcelo Glasberg
- Flutter meets Redux: The Redux way of managing Flutter applications state, by Amir Ghezelbash
-
当 Flutter 遇见 Redux:以 Redux 的风格管理 Flutter 应用的状态 (Flutter meets Redux: The Redux way of managing Flutter applications state),Amir Ghezelbash 著。
Redux and epics for better-organized code in Flutter apps, by Nihad Delic
Fish-Redux
Fish Redux 是一个基于 Redux 状态管理的组合式 Flutter 应用框架,适用于构建中型和大型应用。
Fish Redux is an assembled flutter application framework based on Redux state management. It is suitable for building medium and large applications.
-
由阿里巴巴开发的 Fish-Redux-Library package
Fish-Redux-Library package, by Alibaba
-
Fish-Redux-Source,工程代码
Fish-Redux-Source, project code
-
Flutter-Movie 展示如何使用 Fish Redux 的简单示例应用,包含 30 多个页面、graphql、支付 api 和媒体播放器等。
Flutter-Movie - Non-trivial example demonstrating how to use Fish Redux, with more than 30 screens, graphql, payment api and media player.
BLoC / Rx
基于流/观察者模式的系列。
A family of stream/observable based patterns.
-
使用 BLoC 模式构建你的 Flutter 项目, Sagar Suri 著。
Architect your Flutter project using BLoC pattern, by Sagar Suri
-
BLoC 库 (BLoC Library),Felix Angelov 著。
BloC Library, by Felix Angelov
-
响应式编程 - 流 - BLoC - 用例 (Reactive Programming - Streams - BLoC - Practical Use Cases), Didier Boelens 著。
Reactive Programming - Streams - BLoC - Practical Use Cases, by 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
.
MobX
一个基于观察及响应的状态管理常用库。
A popular library based on observables and reactions.
-
MobX.dart, Hassle free state-management for your Dart and Flutter apps
-
Flutter:使用 MobX 进行状态管理 (Flutter: State Management with Mobx)
Flutter: State Management with Mobx, a video by Paul Halliday
Flutter Commands
基于 ValueNotifiers
的命令式的状态管理,能与 GetIt 完美结合使用,也可以与 Provider
或者其他 locators 配合使用。
Reactive state management that uses the Command Pattern and is based on ValueNotifiers
. Best in combination with GetIt, but can be used with Provider
or other locators too.
- Flutter Command package
-
RxCommand package, 基于
Stream
的实现.
Binder
一个使用 InheritedWidget
作为核心实现的状态管理库。受到 recoil 的启发,该库提供了分治的解决方式。
A state management package that uses InheritedWidget
at its core. Inspired in part by recoil. This package promotes the separation of concerns.
-
Binder snippets 是一个 vscode 插件,能够将代码拆分以获得更高的生产力
Binder snippets, vscode snippets to be even more productive with Binder
GetX
一个简单的响应式状态管理解决方案。
A simplified reactive state management solution.
- GetX package
- Complete GetX State Management, a video by Tadas Petra
- GetX Flutter Firebase Auth Example, by Jeff McMorris
Riverpod
一个编译安全,测试步骤简单的类似于 Provider 的解决方案。且它不依赖于 Flutter SDK。
An approach similar to Provider that is compile-safe and testable. It doesn’t have a dependency on the Flutter SDK.
- Riverpod homepage
- Getting started with Riverpod