deferred_type 3.2.0 icon indicating copy to clipboard operation
deferred_type: ^3.2.0 copied to clipboard

Dart algebraic data type and helper functions for working with asynchronous data.

deferred_type for Dart #

Pub Version GitHub

Modeling asynchronous data in Dart made easy. This package also provides various useful helper functions for transforming and querying the contained values.

Flutter: Package deferred_type_flutter contains FutureBuilder2, an alternative "FutureBuilder" which is simpler to use than the bundled one provided by default in Flutter.

Examples #

final Deferred<String> idle = Deferred.idle();
final Deferred<String> inProgress = Deferred.inProgress();
final Deferred<String> success = Deferred.success('DATA!');
final Deferred<String> error = Deferred.error('ERROR!');

Sample MV app:

import "package:deferred_type/deferred_type.dart";

class App {
  Deferred<String> state = Deferred.idle();

  App() {
    getStatus();
    render();
  }

  Future<void> getStatus() async {
    state = Deferred.inProgress();
    try {
      final someData = await Future.value("some data");
      state = Deferred.success(someData);
    } catch (e) {
      state = Deferred.error(e, null);
    }
    render();
  }

  String view(Deferred<String> state) {
    return state.when(
      success: (data) => "Got data: $data",
      error: (msg, stackTrace) => "Error: $msg",
      inProgress: () => "Loading...",
      idle: () => "Initializing...",
    );
  }

  String render() {
    final template = view(state);
    print(template);
    return template;
  }
}

API Reference #

Detailed API documentation can be found on pub.dev.

Possible to-do #

  • Version with fpdart integration.
  • Come up with a better name for the FutureBuilder.

Other resources #

License #

This project is MIT licensed.

1
likes
140
pub points
0%
popularity

Publisher

unverified uploader

Dart algebraic data type and helper functions for working with asynchronous data.

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

collection

More

Packages that depend on deferred_type