Skip to content

Add a callback to tell you the Future is taking longer than an expected duration to complete. Useful for scenarios where you want to react to or provide feedback for slow running tasks

License

Notifications You must be signed in to change notification settings

6220119/maybe_slow_future

Repository files navigation

maybe_slow_future

pub package codecov Build Status

Add a callback to tell you the future is taking longer than an expected duration to complete.

Usage

To use this plugin, add maybe_slow_future as a dependency in your pubspec.yaml file.

dependencies:
  maybe_slow_future: ^1.0.2

Example

/// Remember to import the package to be able to use the [Future.onSlow] extension method
import 'package:maybe_slow_future/maybe_slow_future.dart';

void main() {
  print('Fetching data...');

  const timeToComplete = Duration(seconds: 5);

  /// simulate a long running operation
  final fetchDataFuture = Future.delayed(timeToComplete, () {
    print('Got data after ${timeToComplete}: Hello World!');
  });

  /// use the extension method [Future.onSlow]
  fetchDataFuture.onSlow(Duration(seconds: 2), () {
    print('Be patient! It might take a while to complete...');
  });

  /// or use the [maybeSlowFuture] function
  maybeSlowFuture(
    actualFuture: fetchDataFuture,
    threshold: Duration(seconds: 3, milliseconds: 500),
    onSlowCallback: () {
      print('This is taking longer than expected...');
    },
  );

  /// output:
  /// Fetching data...
  /// Be patient! It might take awhile to complete...
  /// This is taking longer than expected...
  /// Got data after 0:00:05.000000: Hello World!
}

About

Add a callback to tell you the Future is taking longer than an expected duration to complete. Useful for scenarios where you want to react to or provide feedback for slow running tasks

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published