Event loops (Flutter- Async Programming with Dart) — Part 2

Abdur Mohammed
2 min readAug 2, 2022

--

Part of Flutter/Dart Shorts: To the point| Simple.

1. Prerequisites

  • Read prev article on isolates.
  • Basic understanding of threading.

2. Why is this important?

  • Event loops lie at the base of all of the high level asynchronous programming API.

3. Isolates, Event loops, Queues and their relationship.

  • App -> Isolate -> Event loop -> Event Queue and Microtask Queue

4. What is an event QUEUE?

  • Actions/events/tasks that are queued and waiting to be executed.
  • Example 1: User interaction with the app such as scrolling, button presses etc.
  • Example 2: Messages between isolates.

5. What is Microtask QUEUE?

  • This comes into play when we need to complete a task later and before returning the control to event loop.
  • Warning: Use this for super short tasks only since it blocks the event loop.
  • Example: When mutation changes are reported asynchronously, Microtask queue helps in reporting these changes before the next event is picked up by the loop.

6. What is an event Loop?

  • The event loop job is to pick up items from the queues and execute them one at a time.

7. Okay, Wire-up everything you just said please?

  • First items are picked up from Microtask Queue, executed on first come first serve basis, one at a time.
  • Then the next item from Event Queue is picked up and executed, again in FIFO.
  • Then the cycle repeats. Executes the items in the Microtask Queue and then dequeue next item from the event queue and execute it.

7. What happens when Queues are empty?

  • The event loop just sits there, waiting for items to be added in the queues.

That’s all for this one. Next week — Futures :-)

You can find me on

LinkedInhttps://www.linkedin.com/in/knowabdur
Twitter: — https://twitter.com/AbdurDeveloper

--

--

Abdur Mohammed

React Native| Flutter | Senior Software Engineer | Sydney, Australia