when_async

Contains utility classes to work with asynchronous computations.

Utilities

  • When.future for executing Futures with snapshots.
    • For futures, use execute for one time execution. This only does the asynchronous future computation once and returns the last future's result in subsequent calls. To refresh the future again, use refresh.
    • To get snapshots of the asynchronous computation's state instead, use snapshots. To get refreshed snapshots, use refreshSnapshots.

Usage

A simple usage example:

import 'package:when_async/when_async.dart';

main() {
  final _when = When.future<int>(
    () => Future.delayed(
      const Duration(seconds: 5),
      () => 1,
    ),
  );

  _when.execute(
    onLoading: () => stdout.writeln('Loading'),
    onComplete: (it) => stdout.writeln('$it'),
    onError: (e, s) => stdout.writeln('$e\n$s'),
    onFinally: () => stdout.writeln('Finally'),
  );

  // OR

  _when.snapshots((snapshot) { 
     stdout.writeln('Snapshot: ${snapshot.state}')
  });
}

Check example.dart.

Features and bugs

Please file feature requests and bugs at the issue tracker.

Libraries

when_async