在 Flutter 开发中,状态管理一直是核心话题。从 `setState` 到 `Provider`、`Riverpod`、`Bloc`、`GetX`,各有优劣。今天给大家推荐一个新兴轻量级高性能响应式库 —— RxFlare。

RxFlare 核心亮点:
- 自动依赖追踪 + 字段/索引级精准更新
- 极简 API,低样板代码
- 高性能,适合中大型列表、表单等场景
- 支持 Computed 计算属性、RxEventBus 事件总线、RxFuture 异步封装等

官网地址(pub.dev):https://pub.dev/packages/rxflare)

 一、安装

在 `pubspec.yaml` 中添加依赖:


dependencies:
  flutter:
    sdk: flutter
  rxflare: ^1.5.0    请使用最新版本


然后执行:
flutter pub get


 二、核心概念与基本使用

 1. 响应式变量(.obs)


import 'package:rxflare/rxflare.';

final count = 0.obs;           // int
final name = "张三".obs;       // String
final isActive = true.obs;     // bool


 2. 自动追踪重建 —— `Rx(() => Widget)`


Rx(() => Text("计数: ${count.value}", style: TextStyle(fontSize: 40)));

FloatingActionButton(
  onPressed: () => count.value++,
  child: Icon(Icons.add),
)


一个 `Rx` 块可以同时监听多个变量:


Rx(() => Column(
  children: [
    Text('姓名:${name.value}'),
    Text('计数:${count.value}'),
    Text('状态:${isActive.value ? "激活" : "关闭"}'),
  ],
));


 三、核心竞争力:字段级 / 索引级精准更新

 Map 字段级更新(强烈推荐)


final user = {
  "name": "Tom",
  "age": 25,
  "score": 88,
  "vip": true,
}.obs;   // 或使用 .obsMap

// 不同字段独立监听
Rx(() => Text("姓名: ${user.getItem('name')}"));
Rx(() => Text("年龄: ${user.getItem('age')}"));
Rx(() => Text("分数: ${user.getItem('score')}"));

// 精准更新单个字段(只有依赖该字段的 Rx 会重建)
user.updateField("name", "Jerry");
user.updateField("score", 999);


 List 索引级更新


final items = List.generate(1000, (i) => {
  "id": i,
  "title": "Item $i",
  "score": 50,
}).obsListMap;   // 推荐使用 obsListMap

// 更新指定索引
items.updateAt(5, {...items[5], "score": 999});

// 或使用 updateField(索引作为 key)
items.updateField(10, newValue);


性能优势:1000 项列表中只更新 1 项时,只有对应的 `Rx` 会重建,整个页面不会全部重绘。

 四、计算属性 Computed(超级实用)


final price = 99.obs;
final quantity = 1.obs;

late final total = computed(() => price.value * quantity.value);
late final isExpensive = computed(() => total.value > 500);

Rx(() => Column(
  children: [
    Text("总价: ${total.value} 元", style: TextStyle(fontSize: 32)),
    Text(isExpensive.value ? " 高消费" : "性价比不错"),
  ],
));


支持链式依赖和动态依赖变化,自动追踪。

 五、RxEventBus 事件总线


// 监听
RxEventBus.on<String>(
  module: "chat",
  eventID: 1001,
  callback: (id, uuid, data) async {
    print("收到消息: $data");
  },
);

// 发送
RxEventBus.notify<String>(
  module: "chat",
  eventID: 1001,
  data: "Hello World",
);


支持 Token 批量注销、Sticky 事件、优先级、串/并行执行等高级功能。

 六、异步处理 RxFuture


late RxFuture<User> userFuture = RxFuture(() async {
  await Future.delayed(const Duration(seconds: 2));
  return User(name: "张三");
});

Rx(() {
  if (userFuture.isInitialLoading) return CircularProgressIndicator();
  if (userFuture.hasError) return Text("错误: ${userFuture.error}");
  return Text("用户: ${userFuture.data?.name}");
});


 七、完整示例页面(推荐复制运行)


class BasicDemo extends StatelessWidget {
  final count = 0.obs;
  final user = {"name": "Tom", "age": 25}.obs;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Rx(() => Text("计数: ${count.value}", style: TextStyle(fontSize: 50))),
            Rx(() => Text("姓名: ${user.getItem('name')}")),
            ElevatedButton(
              onPressed: () => count.value++,
              child: Text("加一"),
            ),
            ElevatedButton(
              onPressed: () => user.updateField("name", "Jerry"),
              child: Text("改名"),
            ),
          ],
        ),
      ),
    );
  }
}


 八、其他高级功能

Rx.custom(deps: [count], builder: () => ...)` 手动指定依赖
RxObjMgr` + `RxParent` 控制器依赖管理(类似 GetX 的 Put/Find)
RxAutoDispose` 自动释放
- 内置路由 `rxr`(支持参数传递)
- DevTools 支持

 总结与建议

RxFlare 适合以下场景:
- 追求极致性能和低代码量的项目
- 需要大量字段独立更新的复杂表单/列表页
- 中小型团队,希望状态管理简单且强大的项目

与 GetX 对比:RxFlare 在字段级精准更新上更极致,API 更简洁轻量。

欢迎大家去 pub.dev 试用!有问题可以在下方评论,或者去 GitHub 提 Issue。

完整示例代码可参考官方 Example:https://pub.dev/packages/rxflare/example

(完)

更多推荐