Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Async Intro


Share

Dart Async Intro

Some tasks take time to finish.

For example, reading a file, downloading data from the internet, or waiting for a timer.

If a program stops and waits for each slow task, the whole program freezes.

In an app, this means that buttons and animations stop working while the program waits.

Asynchronous code (often called async code) solves this problem.

Async code starts a slow task, lets the program do other work, and comes back when the task is done.


Synchronous Code

The code you have seen so far is synchronous.

Synchronous means that Dart runs one statement at a time, from top to bottom, and each statement must finish before the next one starts:

Example

void main() {
  print('Boil water');
  print('Make tea');
  print('Drink tea');
}
Try it Yourself »

Result:

Boil water
Make tea
Drink tea

The output always comes in the same order as the code.



Waiting Without Blocking

Future.delayed() runs some code later, after a waiting time.

The waiting time is a Duration, for example Duration(seconds: 1).

Look at the order of the output:

Example

void main() {
  print('Order a pizza');
  Future.delayed(Duration(seconds: 1), () {
    print('The pizza has arrived');
  });
  print('Watch a movie while you wait');
}
Try it Yourself »

Result:

Order a pizza
Watch a movie while you wait
The pizza has arrived

Dart did not stop at Future.delayed().

It started the timer, printed the last line at once, and ran the delayed code one second later.

The program only ends when there is no more waiting work left.


The Event Loop

Dart runs your code on a single thread.

A thread is a worker that runs code, one step at a time.

To handle waiting tasks, Dart uses an event loop.

You can think of the event loop as a to-do list:

  • First, Dart runs all the normal code in main(), from top to bottom.
  • When a slow task (like a timer) is finished, its code is added to the list.
  • When Dart has nothing else to do, it takes the next item from the list and runs it.

This is why the delayed code always runs after the normal code, even with a waiting time of zero:

Example

void main() {
  print('Start');
  Future.delayed(Duration.zero, () {
    print('Delayed code');
  });
  print('End');
}
Try it Yourself »

Result:

Start
End
Delayed code

Order of Output

When you have many delayed tasks, the shortest waiting time finishes first.

The order in the code does not matter, only the waiting time does:

Example

void main() {
  Future.delayed(Duration(seconds: 3), () {
    print('Bus 3 arrived after 3 seconds');
  });
  Future.delayed(Duration(seconds: 1), () {
    print('Bus 1 arrived after 1 second');
  });
  Future.delayed(Duration(seconds: 2), () {
    print('Bus 2 arrived after 2 seconds');
  });
  print('Waiting for the buses...');
}
Try it Yourself »

Result:

Waiting for the buses...
Bus 1 arrived after 1 second
Bus 2 arrived after 2 seconds
Bus 3 arrived after 3 seconds

All three timers run at the same time, so the program takes about 3 seconds in total, not 6.

In the next chapters, you will learn the tools Dart gives you for async code:

  • Futures: a value that will be ready later
  • async and await: a simple way to write code that waits for futures
  • Streams: many values that arrive one after another


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.