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.