services_worker 1.0.1
services_worker: ^1.0.1 copied to clipboard
A minimalistic asynchronous worker that simplifies the boilerplate and execution of tasks.
A minimalistic asynchronous worker.
Features #
This package simplifies the boilerplate and execution of asynchronous tasks
Usage #
import 'package:flutter/material.dart';
import 'package:services_worker/services_worker.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double _data = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(10.0),
child: SingleChildScrollView(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(
height: 15,
),
const Text(
'This is the result of your task',
),
const SizedBox(
height: 25,
),
Text(
_data.toString(),
),
],
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _execute,
tooltip: 'execute',
child: const Icon(Icons.download),
),
);
}
Future _execute() async {
final res = await ServicesWorker.executeInOtherThread(_hardTask, _data);
if (res.hasData) {
final double value = res.data!;
setState(() {
_data = value;
});
return;
}
final error = res.error!;
throw ServicesException.fromServicesError(
error,
);
}
static double _hardTask(double oldValue) {
final double value = oldValue + 1;
final double newValue = ((((value * value) / 2) * (-1 * value)) / 0.777);
return newValue;
}
}
Additional information #
If you like this package and find it usefull, please give it a like.