搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。