狀態 (State) 管理參考
狀態管理是一個相當複雜的話題。如果您在瀏覽後發現一些問題並未得到解答,或者並不適用於您的具體需求場景,自信些,您的實現就是對的。
透過下面的連結瞭解更多的資訊,其中有很多資訊都是由社群(第三方)提供。
總體概覽
在選擇一個具體內容前,您可以先檢視以下幾項。
-
狀態管理的介紹。這是本篇內容的總起。(當您直接跳轉到了該頁面,但錯過了其他頁面時,可以先從這裡開始閱讀)
-
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
適用於較小規模 widget 的暫時性狀態的基礎管理方法。
-
為你的 Flutter 應用新增互動 (Adding interactivity to your Flutter app),一篇 Flutter 的課程。
-
Flutter 中的基礎狀態管理 (Basic state management in Google Flutter),Agung Surya 著。
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 著。
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
一個基於觀察及響應的狀態管理常用庫。
Flutter Commands
基於 ValueNotifiers
的命令式的狀態管理,能與 GetIt 完美結合使用,也可以與 Provider
或者其他 locators 配合使用。
- Flutter Command package
-
RxCommand package, 基於
Stream
的實現.
Binder
一個使用 InheritedWidget
作為核心實現的狀態管理庫。受到 recoil 的啟發,該庫提供了分治的解決方式。
-
Binder snippets 是一個 vscode 外掛,能夠將程式碼拆分以獲得更高的生產力
GetX
一個簡單的響應式狀態管理解決方案。
- GetX package
- Complete GetX State Management, a video by Tadas Petra
- 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.