Futures, async, await: Threading in Flutter
If F=D and D=ST, then F=ST, right?
Flutter is written using Dart and Dart is a single-threaded language then Flutter apps are single-threaded. This means that a Flutter app can only do one thing at a time.
That is all true. But that does not mean that Flutter apps are forced to wait for slower processes.
Flutter apps use an event loop
This should come as no surprise since Android has a main looper and iOS has a run loop (aka. main loop). Heck, even JavaScript devs are unimpressed since JavaScript itself has a … wait for it … event loop. Yes, all the cool kids are using an event loop these days.
An event loop is a background infinite loop which periodically wakes up and looks in the event queue for any tasks that need to run. If any exist, the event loops puts them onto the run stack if and only if the CPU is idle.
As your app is running instructions, they run serially — one after another. If an instruction is encountered that may potentially block the main thread waiting on some resource, it is started and the ‘wait’ part is put on a separate queue.
Why would it wait?
Certain things are slow compared to the CPU. Reading from a file is slow. Writing to a file is…