isolate_functions
A Flutter plugin to run functions in a separate isolate. You can run functions with arguments and return values.
We use this plugin to run functions that take a long time to execute, such as image processing. This plugin is based on flutter_isolate. We simplified the usage of the plugin and added the ability to run functions with arguments and return values.
Some heavy computation functions may take a long time to execute. If you run them in the main isolate, the UI will freeze. Then you can simply pass the function to the plugin and it will run it in a separate isolate. The plugin will return the result of the function execution.
You will pass the function and if you have parameters you will pass them in a map. The function will work in Isolate and return the result.
await IsolateFunctions().isolate(Calculator().addOne, paramsMapIn:{'int':1}).then((value) => print(value));
/// A Calculator.
class Calculator {
/// Returns [value] plus 1.
int addOne(Map? paramsMapIn) {
/// Heavy lifting.
var total = 0;
for(var i = 0; i < 1000000000; i++) {
total += i;
}
return paramsMapIn!['int'] + 1;
}
}
You can use quite to simply quit the current isolate. And use quitAll to quit all isolates.
Run this command:
With Flutter:
$ flutter pub add isolate_functions
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get
):
dependencies:
isolate_functions: ^1.0.3
Alternatively, your editor might support flutter pub get
. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:isolate_functions/isolate_functions.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:isolate_functions/isolate_functions.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int _valueAdded = 0;
final _isolateFunctionsPlugin = IsolateFunctions();
@override
void initState() {
super.initState();
iniValueCalculation();
}
// Value is calculated in a separate isolate.
// Obviously, this is a trivial example, but it demonstrates the concept.
// You can pass in any function that returns a value.
Future<void> iniValueCalculation() async {
int? value;
// Calculation messages may fail, so we use a try/catch.
// We also handle the message potentially returning null.
//the input is 1 and you should expect 2.
try {
value =
await _isolateFunctionsPlugin.isolate(Calculator().addOne, paramsMapIn:{'int':1}) ?? 'Unknown platform version';
} on PlatformException {
throw 'Failed to get platform version.';
}
// If the widget was removed from the tree while the calculation was in
// flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_valueAdded = value!;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Result of Calculation is: $_valueAdded\n'),
),
),
);
}
}
/// A Calculator.
class Calculator {
/// Returns [value] plus 1.
int addOne(Map? paramsMapIn) {
/// Heavy lifting.
for(var i = 0; i < 1000000000; i++) {
}
return paramsMapIn!['int'] + 1;
}
}
Download details:
Author: nikaeene