Skip to content

Flutter's compute function made available for all non-Flutter Dart programs

License

Notifications You must be signed in to change notification settings

dartsidedev/compute

Repository files navigation

compute

Flutter's compute function made available for all non-Flutter Dart programs

The compute package takes Flutter's compute function and makes it available for all Dart programs.

Continuous Integration compute Published by dartside.dev GitHub Stars Count

Important links

If you enjoy using this package, a thumbs-up on pub.dev would be highly appreciated! 👍💙

Motivation

When working with isolates, wiring up the SendPorts, the ReceivePorts correctly is a lot of boilerplate code when all you want to do is spawn an isolate, compute something, and use the computed value.

Flutter's compute function is a very useful abstraction over isolates that can be useful in all kinds of Dart apps.

Unfortunately, Flutter's compute function is not available for a Dart package that doesn't use Flutter, for example command-line and server-side applications.

This package addresses this issue. It extracts the compute function from Flutter and makes it available for all Dart projects, so if you wish to perform some computation on a separate isolate and use its return value, now you can!

Disclaimers

Do not assume that using compute will automatically speed up your code: you should benchmark your Dart applications with and without the compute function, and only switch to using compute if it really speeds up your application.

Keep in mind, that by using compute, you lose some flexibility that working directly with isolates would enable you.

The package is safe to be used on web, but there will be no real isolates spawned.

Changes are synced with Flutter's stable branch only (and they are currently synced manually).

Usage

Dart

This package works everywhere and doesn't have any Flutter-specific dependency.

import 'package:compute/compute.dart';

int square(int a) => a * a;

Future<void> main() async {
  final squared = await compute(square, 5);
  print('5^2=$squared');
}

Flutter

If you are on a Flutter project, you don't need this package. This package should only be used in environments where you cannot use Flutter's compute function.

For your Flutter project, use the compute function directly from Flutter's foundation.

About

Flutter's compute function made available for all non-Flutter Dart programs

Topics

Resources

License

Stars

Watchers

Forks

Languages