deferred_type 3.2.0 copy "deferred_type: ^3.2.0" to clipboard
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
160
points
77
downloads

Publisher

unverified uploader

Weekly Downloads

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

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

collection

More

Packages that depend on deferred_type