搜索插件、代码仓与 APP 分类

插件详情v2.3.0

rx_notifier

通过透明应用函数式响应式编程(TFRP)扩展 ValueNotifier

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2780
喜欢数
82
Pub 分数
140

安装插件

flutter pub add rx_notifier

插件介绍

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

RxNotifier 是一个 Flutter/Dart 包,它扩展了 ValueNotifier,实现了透明的函数式响应式编程(TFRP)。它通过允许开发者使用函数式操作符组合和转换状态变化,实现响应式状态管理,同时保持 ValueNotifier 的简洁性。该包可无缝集成到现有的 Flutter 应用中,提供一种轻量级的状态管理方案,支持响应式模式且无需样板代码。它支持所有主要平台,并通过不可变更新和类似流的转换,促进编写干净、可测试的代码。

适合场景

  • 响应式 UI 状态更新
  • 组合复杂的状态逻辑
  • 替换多个 ValueNotifiers
  • 简化状态转换
  • 构建响应式表单
  • 管理异步状态

主要特点

  • 函数式响应式编程
  • 透明的 TFRP 集成
  • 不可变状态更新
  • 流式操作符
  • 最小化样板代码
  • 跨平台支持

适合对象

  • 寻求响应式状态管理的开发者
  • 需要可组合状态逻辑的应用程序
  • 偏好函数式编程的团队
  • 避免使用重型状态管理的项目
  • Flutter 初学者学习响应式模式
  • 需要轻量级状态解决方案的应用程序

使用建议

  • 需要理解函数式编程概念
  • 可能为初学者引入学习曲线
  • 仅限基于 ValueNotifier 的响应式模式
  • 不适合大规模状态管理
  • 最适合中等复杂度的状态需求
  • 不会取代完整的响应式框架(如 RxDart)

原始 README

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

rx_notifier

The ValueNotifier is a simple, native form of Flutter reactivity. This extension aims to transparently apply functional reactive programming (TFRP).

Implementing the Atomic State.

It is possible to implement Recoil Atoms pattern using RxNotifier. This pattern consists of the state being an object with its own reactivity.

atom

Motivation

Developers still have trouble understanding state management in Flutter. We had this conclusion after several research in the community fluttering and also in partner companies. Atomic State is a noob-friendly state management approuch at the same time that maintains a reliable structure thinking of scalability and maintenance.

More details, read this Medium article on the subject.

Rules

We must take into account some architectural limits to execute this Approach:

  1. All states must be an atom(RxNotifier instance).
  2. All actions must be an atom(RxNotifier instance).
  3. Business rules must be created in the Reducer and not in the Atom.

Layers

We will have 3 main layers, they are: Atoms, Reducers and Views;

atom

Note that the View (which is the presentation layer) does not know about the Reducer (which is the business rule execution layer). These two layers share atoms that in turn represent the state and the dispatch of state actions.

In Flutter these layers translate to Atom(RxNotifier), Reducer(RxReducer) and View(Widget):

atom

Atoms (RxNotifier)

Atom represent the reactive state of an application. Each atom has its own reactivity.

// atoms
final productsState = <Product>[].asRx();
final productTextFilterState = RxNotifier<String>('');

// computed
List<Product> get filteredProductsState {
     if(productTextFilterState.value.isEmpty()){
         return productsState.value;
     }

     return productsState.where(
         (p) => p.title.contains(productTextFilterState.value),
     );
}

// actions
final selectedProductState = RxNotifier<Product?>(null);
final fetchProductsState = RxNotifier(null);

Reducer (RxReducer)

In this architecture you are forced to split state management of business rules, which may seem strange at first when seen that we are always managing and reducing state in the same layer as BLoC and ChangeNotifier for example.
However, dividing state management and business rule execution will help us distribute multiple states to the same widget, and these multiple states will not need to be concatenated beforehand through a facade or proxy.

The layer responsible for making business decisions will be called Reducer:

class ProductReducer extends RxReducer {

    ProductReducer(){
        on(() => [fetchProductsState.action], _fetchProducts);
        on(() => [selectedProductState.value], _selectProduct);
    }

    void _fetchProducts(){
        ...
    }

    void _selectProduct(){
        ...
    }
}

Reducers can register methods/functions that listen to the reactivity of an Atom(RxNotifier).

View (Widget)

Any widget can listen to changes of one or many atoms, provided they have the RxRoot widget as their ancestor. For more details about RxRoot, Read the documentation.

The context.select() method is added via Extension to BuildContext and can be called on any type of Widget, StatefulWidget and StatelessWidget.


...
Widget build(BuildContext context){
     final products = context.select(
                 () => filteredProductsState.value
              );
     ...
}

Examples

Flutter projects using RxNotifier

Trivial Counter.

Shop Cart.

所属分类

依赖

依赖清单 2 项

  • collection^1.17.0
  • rx_notifier_annotation>=1.0.0 <2.0.0

源码仓库

使用此插件的项目

暂无相关数据。