Run your code in background easily with Flutter

An Lam
FlutterVN
Published in
2 min readAug 12, 2019

Coding with Flutter is so easy, you can make a beautiful application with a very short time. Everything is a widget, so you can custom everything. That’s a main reason why I choose Flutter for developing cross platform application. At the beginning, I consider whether Flutter can run code in background thread, because it’s very important when making an app which need high performance such as: JSON decoding, encryption, image processing, calling network,… Luckily, Dart have Isolate which is a worker process you can create to execute code but does not share memory.

If you need to run a method in the background that just needs the return value you can use compute, for instance, decode a JSON, return otherwise, use Isolate. For more details, refer to this post.

However, you need to setup a lot of codes before you can use run your code in other Isolate. Thanks to Dreck for making very useful library named Worker for easy using. But with current source, you can’t get progress call back in case you upload/download file and cancel upload/download file. I’ve updated some pieces of code to improve for these cases. In the example, i made 3 tabs :

  • Normal API: using Isolate to call normal API.
  • Download: download file and get progress call back.
  • Cancel: same Download + cancel download.

Upload file is same as Download file.

Please refer to my source on Github for more details:

--

--