Flutter & Dart (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How does the Dart Event Loop work (Microtask Queue vs Event Queue)?
senior LevelMost AskedCode ExampleEvent LoopSystem DesignData Structures
In Simple Words: Dart is single-threaded and runs an event loop with two FIFO queues:
- Microtask Queue (High Priority): Processes critical internal asynchronous tasks before returning control to the event queue (e.g.
scheduleMicrotask()). - Event Queue (Standard Priority): Handles external events: user touch gestures, timer ticks, drawing frames, network I/O, file reads.
- Execution Order: The event loop executes ALL microtasks until empty, then processes ONE item from the event queue, then checks the microtask queue again.
text
Dart Event Loop Priority:
[Event Loop] ──► Check [Microtask Queue] ──► (Drain All Microtasks)
│ (When Empty)
▼
Process ONE item from [Event Queue] ──► Loop!