패키지, 저장소 및 앱 카테고리 검색

패키지 상세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와의 통합
  • Futures에 대한 스트림과 유사한 동작
  • 자동 상태 전파
  • 로딩, 성공, 오류 상태를 지원합니다
  • 최소한의 보일러플레이트

적합한 대상

  • GetX 사용자가 반응형 Futures 찾고 있음
  • 실시간 비동기 피드백이 필요한 앱
  • 개발자들이 수동적인 Future 처리를 피하고 있다
  • 자주 API 또는 비동기 데이터 가져오기를 수행하는 프로젝트
  • 청결한 상태 관리를 우선시하는 팀

고려 사항

  • GetX 프레임워크 필요
  • GetX 생태계에 국한
  • 간단한 Future에 오버헤드를 추가할 수 있습니다
  • 반응하지 않는 워크플로우에는 적합하지 않습니다
  • 자주 비동기 상태가 변경되는 앱에서 가장 잘 사용됩니다

원본 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 개

소스 저장소

이 패키지를 사용하는 프로젝트

아직 데이터가 없습니다.