搜索分类、插件与代码仓

插件详情v4.1.3

flutter_flux

Flutter 的 Flux 库,具有单向数据流,灵感来自 Reflux 和 Facebook 的 Flux 架构。

近 30 天下载
55
喜欢数
9
Pub 分数
40

安装插件

flutter pub add flutter_flux

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Flutter Flux 是一个为 Flutter 设计的状态管理包,实现了单向数据流,灵感来自 Facebook 的 Flux 和 Reflux 模式。它通过操作、存储和视图促进可预测的状态变化,实现关注点的清晰分离。该库强调简洁性和可扩展性,适用于中大型应用,其中状态一致性和调试至关重要。通过极少的样板代码,它使用提供的命令行工具可无缝集成到 Flutter 项目中。

适合场景

  • 管理复杂的应用程序状态
  • 构建可扩展的 Flutter 应用程序
  • 强制执行可预测的状态转换
  • 使用清晰的流程调试状态更改
  • 使用一致的数据流实现响应式UI

主要特点

  • 单向数据流
  • 基于操作的状态更新
  • 用于状态管理的状态存储抽象
  • 与 Flutter widget 的简单集成
  • 对异步操作的支持

适合对象

  • 熟悉 Flux 架构的开发者
  • 需要严格状态控制的项目
  • 注重可维护状态逻辑的团队
  • 中大型 Flutter 应用
  • 需要清晰调试路径的应用程序

使用建议

  • 初学者的学习曲线更陡峭
  • 需要理解 Flux 概念
  • 不如一些现代的状态管理解决方案灵活
  • 与替代方案相比,社区支持有限
  • 不太适合状态极少的简单应用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

flutter_flux

A Dart app architecture library with uni-directional data flow inspired by RefluxJS and Facebook's Flux.

This is an experimental package and does not have official support from the Flutter team. However, feedback is most welcome!


Overview

flux-diagram

flutter_flux implements a uni-directional data flow pattern comprised of Actions, Stores, and StoreWatchers. It is based on w_flux, but modified to use Flutter instead of React.

  • Actions initiate mutation of app data that resides in Stores.
  • Data mutations within Stores trigger re-rendering of app view (defined in StoreWatcher).
  • Flutter Widgets and other interaction sources dispatch Actions in response to user interaction.
  • and the cycle continues...

What's Included

Action

An Action is a command that can be dispatched (with an optional data payload) and listened to.

In flutter_flux, Actions are the sole driver of application state change. Widgets and other objects dispatch Actions in response to user interaction with the rendered view. Stores listen for these Action dispatches and mutate their internal data in response, taking the Action payload into account as appropriate.

import 'package:flutter_flux/flutter_flux.dart';

// define an action
final Action<String> displayString = new Action<String>();

// dispatch the action with a payload
displayString('somePayload');

// listen for action dispatches
displayString.listen(_displayAlert);

_displayAlert(String payload) {
  print(payload);
}

BONUS: Actions are await-able!

They return a Future that completes after all registered Action listeners complete. It's NOT generally recommended to use this feature within normal app code, but it is quite useful in unit test code.

Store

A Store is a repository and manager of app state. The base Store class provided by flutter_flux should be extended to fit the needs of your app and its data. App state may be spread across many independent stores depending on the complexity of the app and your desired app architecture.

By convention, a Store's internal data cannot be mutated directly. Instead, Store data is mutated internally in response to Action dispatches. Stores should otherwise be considered read-only, publicly exposing relevant data ONLY via getter methods. This limited data access ensures that the integrity of the uni-directional data flow is maintained.

A Store can be listened to to receive external notification of its data mutations. Whenever the data within a Store is mutated, the trigger method is used to notify any registered listeners that updated data is available. In flutter_flux, StoreWatchers listen to Stores, typically triggering re-rendering of UI elements based on the updated Store data.

import 'package:flutter_flux/flutter_flux.dart';

class RandomColorStore extends Store {

  // Public data is only available via getter method
  String _backgroundColor = 'gray';
  String get backgroundColor => _backgroundColor;

  // Actions relevant to the store are passed in during instantiation
  RandomColorActions _actions;

  RandomColorStore(RandomColorActions this._actions) {
    // listen for relevant action dispatches
    _actions.changeBackgroundColor.listen(_changeBackgroundColor);
  }

  _changeBackgroundColor(_) {
    // action dispatches trigger internal data mutations
    _backgroundColor = '#' + (new Random().nextDouble() * 16777215).floor().toRadixString(16);

    // trigger to notify external listeners that new data is available
    trigger();
  }
}

BONUS: Stores provide an optional terse syntax for action -> data mutation -> trigger operations.

// verbose syntax
actions.incrementCounter.listen(_handleAction);

_handleAction(payload) {
    // perform data mutation
    counter += payload;
    trigger();
  }

// equivalent terse syntax
triggerOnAction(actions.incrementCounter, (payload) => counter += payload);

Examples

Simple examples of flutter_flux usage can be found in the example directory. The example README includes instructions for building / running them.


External Consumption

flutter_flux implements a uni-directional data flow within an isolated application or code module. If flutter_flux is used as the internal architecture of a library, this internal data flow should be considered when defining the external API.

  • External API methods intended to mutate internal state should dispatch Actions, just like any internal user interaction.
  • External API methods intended to query internal state should leverage the existing read-only Store getter methods.
  • External API streams intended to notify the consumer about internal state changes should be dispatched from the internal Stores, similar to their triggers.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。