Is Flutter single thread?

Flutter is mainly single thread. Why so? Because Dart language is a single threaded language. However, Flutter uses several threads to do its work.

Does it sound confusing?

Don’t worry. Basically, in Flutter all of our Dart code runs on User Interface thread. In spite of that, it uses other threads besides that. Just to name a few, there are Platform thread, I/O thread, etc.

On the contrary Android applications are not single threaded. Besides the main thread Android applications can run the heavy jobs in worker or background thread.

What do we mean by heavy jobs?

Well, let me explain. In any mobile application, users need to accomplish many tasks simultaneously. Consider an event like button click. That might do some small logical operations, such as get the result of a small addition. The main thread can handle such small operations and doesn’t take much time.

However, an image to be opened using a network, or downloading a file isn’t a small operation. As a result, the main thread needs to do the heavy lifting. And, that might halt the whole program.

However, Dart and Flutter have its answer. They together perform long-running operations with the help of Future API, async, await keywords, and then functions.

Together they perform asynchronous programming in Flutter.

Asynchronous doesn’t mean multi-threaded. Basically it means the code shouldn’t run at the same time and the job of main thread shouldn’t be burdensome.

In case of Flutter, asynchronous means that an operation is scheduled to be run on the same thread after other tasks have finished.

With reference to asynchronous programming we need to understand isolate also. In a separate section, we’ll discuss isolate.

When the conception of isolate comes into the picture, Dart no longer remains single threaded language. Because it can create separate isolate. Although within an isolate Dart again runs on a single thread.

Future in Flutter or Dart gives us a promise token and says that a value will be returned at some point in future.

It never says in which thread it will have its job done at that certain point.

This part is little tricky. However, we need to see how we can write a simple Dart code to understand this concept.

import 'dart:async';

void main(List<String> args) {
  print('main thread starts >>>>>');
  print('main process starts >>>>>');

  openImage();

  print('main process ends and starts counting seconds... >>>>>');
}

void openImage() async {
  var imageFile = await downloadImage();
  print('The downloaded file is --> $imageFile');
  print('main thread ends after 10 seconds....');
}

Future<String> downloadImage() {
  var image = Future<String>.delayed(Duration(seconds: 10), () {
    return 'Here is an imaginative image downloaded.';
  });
  return image;
}

Let’s try to understand the above code.

Depending on that, as a consequence, we can later create a database operations in Flutter. Remember, database queries takes time and we must do using Future.

Future works on <T>, or type. In our case, we’ve used String type and returns a text after 10 seconds.

However, when we run the program, the default process starts the main thread. And it prints, main thread starts and main process starts.

Then after 10 seconds we get the result that our Future object returns. And at the same time, the main thread closes down.

Let’s run and watch the output, so that everything makes sense.

main thread starts >>>>>
main process starts >>>>>
main process ends and starts counting seconds... >>>>>
The downloaded file is --> Here is an imaginative image downloaded.
main thread ends after 10 seconds....
Exited

To sum up, here a Future object has produced a value of type String. A Future object belongs to any one of the states – either it’s completed, or it’s uncompleted.

When the main thread starts and we call a Future, it queues up work and returns an uncompleted state.

But we don’t see that part in our bare eyes.

When the Future’s operation is finished, it returns a completed state with a value of same type.

If it cannot, Future completes with an error.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

GitHub repository

Technical blog

Twitter

Comments

8 responses to “Is Flutter single thread?”

  1. […] We’ve discussed this feature for absolute beginners in previous section, is Flutter single thread? […]

  2. […] discussed Future, await and async for absolute beginners in previous section, is Flutter single thread? Therefore, if you’re a beginner, you might take a look before we […]

  3. […] discussed this feature for absolute beginners in previous section, is Flutter single thread? If you’re a complete beginner searching to know about Future in Flutter, please check […]

  4. […] await and async first. As we’ve discussed Future, await and async for absolute beginners in previous section- is Flutter single thread? Therefore, if you’re a beginner, you might take a look before we proceed towards the final […]

  5. […] await and async first. As we’ve discussed Future, await and async for absolute beginners in previous section- is Flutter single thread? Therefore, if you’re a beginner, you might take a look before we proceed towards the final […]

  6. […] We’ve discussed Future, await and async for absolute beginners in previous section, is Flutter single thread? […]

  7. […] We’ve discussed this feature for absolute beginners in previous section, is Flutter single thread? […]

  8. […] have learned that Flutter and Dart is single thread. It does all the work on its main thread. It has no worker thread that runs […]

Leave a Reply