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

GitHub 代码仓tareq-alsayed/rx_future

rx_future

一个强大的库,将 Getx Rx 集成以引入一种新类型 RxFuture,使处理未来状态(加载、错误)和获取该未来的结果变得简单,并提供取消未来的功能。

rx_future 项目封面
星标
7
复刻
0
最近推送(UTC)
2022年10月4日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

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.

tareq-alsayed GitHub avatar
GitHub 作者

tareq-alsayed ↗

语言C++DartCMakeHTMLCSwiftKotlinObjective-C

此仓库发布的插件

使用的依赖

依赖清单 5 项

  • flutter{"sdk":"flutter"}
  • get^4.6.1
  • async^2.8.2
  • flutter_test开发依赖{"sdk":"flutter"}
  • flutter_lints开发依赖^1.0.0

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗