狀態 (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.