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 Futures


Share

Dart Futures

A Future is a value that is not ready yet, but will be ready later.

You can think of it like a receipt from a restaurant: you do not have your food yet, but you will get it when it is ready.

A future is always in one of these states:

  • Uncompleted: the task is still running
  • Completed with a value: the task worked, and the result is ready
  • Completed with an error: the task failed

The type of a future says what kind of value it will give you.

For example, Future<String> will give you a String, and Future<int> will give you an int.


Create a Future

Future.delayed() creates a future that completes after a waiting time.

The value that the function returns becomes the value of the future.

If you print the future itself, you do not see the value, only the type of the future:

Example

void main() {
  Future<String> order = Future.delayed(Duration(seconds: 1), () {
    return 'Pizza';
  });
  print(order);
}
Try it Yourself »

Result:

Instance of 'Future<String>'


Get the Value With then()

The then() method runs a function when the future completes with a value.

The value is passed to the function as a parameter:

Example

void main() {
  Future<String> order = Future.delayed(Duration(seconds: 1), () {
    return 'Pizza';
  });
  order.then((food) {
    print('Your $food is ready');
  });
  print('Waiting for the order...');
}
Try it Yourself »

Result:

Waiting for the order...
Your Pizza is ready

Notice that Waiting for the order... is printed first, because the code after then() does not wait.

You can chain then() calls.

The value that one then() returns is passed to the next one:

Example

void main() {
  Future.delayed(Duration(seconds: 1), () => 5)
      .then((price) => price * 2)
      .then((total) {
    print('Total: $total dollars');
  });
}
Try it Yourself »

Result:

Total: 10 dollars

Handle Errors With catchError()

If the task throws an exception, the future completes with an error.

The catchError() method runs a function when this happens:

Example

void main() {
  Future<String> order = Future.delayed(Duration(seconds: 1), () {
    throw Exception('The kitchen is closed');
  });
  order.then((food) {
    print('Your $food is ready');
  }).catchError((error) {
    print('Something went wrong: $error');
  });
}
Try it Yourself »

Result:

Something went wrong: Exception: The kitchen is closed

The function inside then() did not run, because the future did not get a value.

Note: If a future fails and nothing handles the error, the program stops with an error message.


A Future That Is Already Done

Future.value() creates a future that already has a value.

This is useful when a function must return a future, but the value is already known:

Example

void main() {
  Future<int> score = Future.value(100);
  score.then((value) {
    print('Score: $value');
  });
  print('This line is printed first');
}
Try it Yourself »

Result:

This line is printed first
Score: 100

Even when the value is ready, the function in then() still runs after the normal code.


Wait for Many Futures

Future.wait() takes a list of futures and returns one new future.

The new future completes when all the futures are done, and its value is a list with all the results, in the same order as the input list:

Example

void main() {
  Future<String> drink = Future.delayed(Duration(seconds: 2), () => 'Coffee');
  Future<String> food = Future.delayed(Duration(seconds: 1), () => 'Sandwich');

  Future.wait([drink, food]).then((results) {
    print(results);
    print('Breakfast is ready');
  });
}
Try it Yourself »

Result:

[Coffee, Sandwich]
Breakfast is ready

Both futures run at the same time, so this takes about 2 seconds, not 3.

In the next chapter, you will learn async and await, which make code with futures easier to read.



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.