https://github.com/fluttervn/isolate_woker
Library help run flutter tasks in other isolate
https://github.com/fluttervn/isolate_woker
android background-thread background-worker backgroundtask flutter ios isolate
Last synced: about 1 month ago
JSON representation
Library help run flutter tasks in other isolate
- Host: GitHub
- URL: https://github.com/fluttervn/isolate_woker
- Owner: fluttervn
- License: mit
- Created: 2019-08-01T07:02:09.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-02T04:56:31.000Z (over 5 years ago)
- Last Synced: 2024-11-03T21:35:45.502Z (6 months ago)
- Topics: android, background-thread, background-worker, backgroundtask, flutter, ios, isolate
- Language: Dart
- Size: 621 KB
- Stars: 55
- Watchers: 4
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# isolate_worker
[](https://travis-ci.org/fluttervn/isolate_woker)
Library help run flutter tasks in other isolate. The library improve from original library: https://github.com/Dreckr/Worker
Improvement:
- Return callback progress for upload/download file
- Support cancel download/upload file## Usage
Define class:
```dart
class DownloadTask implements FileTask> {
final Dio dio;
final String url;
final String savePath;
CancelToken cancelToken;DownloadTask(
{@required this.dio, @required this.url, this.savePath, this.taskId});@override
Future execute() {
return _doExecute();
}Future _doExecute() async {
cancelToken = CancelToken();
var completer = Completer();try {
final response = await dio.download(url, savePath,
cancelToken: cancelToken, onReceiveProgress: taskProgressCallback);print('DownloadTask success: $response');
completer.complete(true);
} catch (e) {
print('DownloadTask error: $e');
completer.completeError(e);
}return completer.future;
}@override
ActionType actionType = ActionType.download;@override
String taskId;@override
var taskProgressCallback;@override
void handleCancel(String taskId) {
cancelToken?.cancel('Cancel download $taskId');
}
}
```
Use:
```dart
Function(TransferProgress progress) progressCallback = (progress) {
setState(() {
percent = progress.count / progress.total;
});
};var saveFolder = await Utils.getDownloadDirectory('Demo/Download');
final fullPath = '${saveFolder.path}/download.jpg';
final urlPath = 'https://sample-videos.com/img/Sample-jpg-image-2mb.jpg';
final dio = Dio();
var downloadTask = DownloadTask(
taskId: fullPath, dio: dio, url: urlPath, savePath: fullPath);
final worker = Worker(poolSize: 1);
await worker.handle(downloadTask, callback: progressCallback);
setState(() {
loading = false;
imagePath = fullPath;
});
```
Please refer to [my post on Medium](https://medium.com/fluttervn/run-your-code-in-background-easily-with-flutter-2e98d94f30de?source=friends_link&sk=2be1198ef7b5a5cb8191117e7339f536) for more details## Authors
- [anlam87](https://github.com/anticafe) ([email protected])