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

插件详情v1.0.3

rx_future

引入 GetX 中一种新类型 RxFuture,用于监听 Future 状态。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
30
喜欢数
33
Pub 分数
160

安装插件

flutter pub add rx_future

插件介绍

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

RxFuture 是一个 Flutter/Dart 包,引入了一种新的响应式类型 RxFuture,使开发者能够轻松监听异步操作的状态。该包专为 GetX 集成而设计,可在 Future 完成、成功或失败时提供实时更新。通过为 Futures 提供类似流的接口,它增强了状态管理,简化了 UI 更新,无需手动轮询或复杂的样板代码。适用于响应式应用架构,能提升响应速度并减少处理异步数据时的代码复杂性。

适合场景

  • 监听异步操作状态
  • 基于 Future 完成的响应式 UI 更新
  • 简化错误和加载状态
  • 与 GetX 状态管理集成
  • 使用实时反馈处理 API 调用

主要特点

  • 响应式 Future 状态监听
  • 与 GetX 的集成
  • Future 的流式行为
  • 自动状态传播
  • 支持加载、成功和错误状态
  • 最小化样板代码

适合对象

  • GetX 用户寻求响应式 Futures
  • 需要实时异步反馈的应用
  • 开发人员避免手动处理 Future
  • 频繁进行 API 或异步数据获取的项目
  • 注重清洁状态管理的团队

使用建议

  • 需要 GetX 框架
  • 仅限 GetX 生态系统
  • 可能为简单的 Futures 增加开销
  • 不适用于非响应式工作流
  • 最适合用于具有频繁异步状态更改的应用程序

原始 README

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

RxFuture

A powerful library that integrate Getx Rx to introduce a new type called RxFuture which make it easy to handle future states (loading, error) and get the result of this future, with some functionality like canceling the future.

Get started

Add dependency
dependencies:
  rx_future: ^1.0.2
Super simple to use

1- import rx_future and get state manager then create your own GetxController.

import 'package:get/state_manager.dart';
import 'package:rx_future/rx_future.dart';

class MyController extends GetxController {}

2- declare your rxFuture, and pass initial value.

class MyController extends GetxController {
    RxFuture<Map<String, dynamic>> state = RxFuture({});
}

3- define your functions to trigger the future

class MyController extends GetxController {
  RxFuture<Map<String, dynamic>> state = RxFuture({});

  Future<void> getData() async {

  }
}

4- now trigger the state by passing a future callback to the observe method.

class MyController extends GetxController {
  RxFuture<Map<String, dynamic>> state = RxFuture({});

  Future<void> getData() async {
    // you have access to value here, so you can maybe depend on it to get a new data.
    await state.observe((value) async {
      // this callback should return Future<T> or
      // Future<Map<String , dynamic>> in our case.
      return await myApi();
    });
  }
}

5- now you can use state.error & state.loading by listening to them in your ui code


Obx(() {
    // handle loading state.
    if(myController.state.loading){
        return const CircularProgressIndicator();
    }

    // handle error state
    if(myController.state.hasError){
        return Text(myController.state.error.toString());
    }

    // you have result to show.
    return MyWidget(myController.state.result);

})

That was so easy, wasn't it?

Declare more than one RxFuture for multiple state to handle.

More

let's have a closer look on more functionality

1- hooks there is many hooks that would be triggered in different situations, here is an example:

await state.observe(
  (value) async {
    return await myApi();
  },
  // triggered once our future is completed.
  onSuccess: (value) {
     print("success $value");
  },

  // triggered once our future throw an error, providing the error.
  onError: (e) {},

  // triggered when our future canceled by .cancel() method.
  onCancel: () {},
);

for example canceling your future is easy now, just call .cancel() method on your RxFuture

void cancel(){
  state.cancel();
}

the above code would cancel any running future in this RxFuture, and would trigger onCancel method.

What happen on multiple calls? what would happen to my state

by default when you call state.observe() more than one time before it is completed, it would ignore the new calls, always changing your state due to your first call until it will completed.

you can change this behavior be passing MultipleCallsBehavior param to the observe method

state.observe(
  (val) async {
    return await myApi();
  },
  // the default, ignoring new calls until the first call completed.
  multipleCallsBehavior: MultipleCallsBehavior.abortNew,
)

to ignore old calls and refresh your state due to your new calls use MultipleCallsBehavior.abortOld

state.observe(
  (val) async {
    return await myApi();
  },
  // the default, ignoring old calls and refresh the state due to new call.
  multipleCallsBehavior: MultipleCallsBehavior.abortOld,
)

calling .observe more than one when loading would trigger onMultipleCalls hook

await state.observe(
  (value) async {
    return await myApi();
  },
  // triggered once our future is completed.
  onMultipleCalls: () {},
);

❤️ Found this project useful?

If you found this project useful, then please consider giving it a ⭐ on Github and sharing it with your friends via social media.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。