Buscar pacotes, repositórios e categorias de apps

Detalhes do pacotev1.0.3

rx_future

Apresentando um novo tipo no GetX chamado RxFuture para escutar o estado do Future.

AndroidiOSWebmacOSWindowsLinux
Downloads em 30 dias
30
Curtidas
33
Pontos Pub
160

Instalar pacote

flutter pub add rx_future

Visão geral do pacote

Orientação gerada a partir de informações públicas. Apenas para referência.

RxFuture é um pacote Flutter/Dart que introduz um novo tipo reativo, o RxFuture, permitindo que desenvolvedores escutem facilmente o estado de operações assíncronas. Projetado com integração ao GetX em mente, permite atualizações em tempo real quando um Future é concluído, bem-sucedido ou falha. Isso melhora a gestão de estado fornecendo uma interface semelhante a stream para Futures, simplificando as atualizações da IU sem necessidade de varredura manual ou código boilerplate complexo. Ideal para arquiteturas de aplicativos reativos, melhora a responsividade e reduz a complexidade do código ao lidar com dados assíncronos.

Casos de uso

  • Ouvindo os estados de operação assíncrona
  • Atualizações de interface reativas com base na conclusão do Future
  • Simplificando estados de erro e carregamento
  • Integração com gerenciamento de estado GetX
  • Manipulando chamadas de API com feedback em tempo real

Principais recursos

  • Escuta de estado assíncrono reativo
  • Integração com GetX
  • Comportamento semelhante a stream para Futures
  • Propagação automática de estado
  • Suporta estados de carregamento, sucesso e erro
  • Boilerplate mínimo

Indicado para

  • Usuários do GetX procurando Futures reativos
  • Aplicativos que precisam de feedback assíncrono em tempo real
  • Desenvolvedores evitando o manuseio manual de Future
  • Projetos com busca frequente de API ou dados assíncronos
  • Equipes que priorizam a gestão de estado limpo

Considerações

  • Requer framework GetX
  • Limitado ao ecossistema GetX
  • Pode adicionar sobrecarga para Futures simples
  • Não adequado para fluxos de trabalho não reativos
  • Melhor usado em aplicativos com mudanças frequentes de estado assíncrono

README original

Snapshot do README original do repositório-fonte vinculado. Consulte o repositório para o conteúdo mais recente.

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.

Categorias

Dependências

Lista de dependências 2 itens

Repositório-fonte

Projetos que usam este pacote

Ainda não há dados.