Future Use a Future to handle a single async event

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
Get Unlimited PRO Access


*Enrollment provides full access to this course (and updates) for life.

Create a Future

Many APIs in Dart/Flutter return Futures. To simulate an async event, we can create a future that will resolve after a 5 second delay.

file_type_dartlang futures.dart
var delay = Future.delayed(Duration(seconds: 5));

Handle a Future

A future can either be a success or a error. Use then then to handle a successful resolution and catchError to handle an error.

file_type_dartlang futures.dart
      .then((value) => print('I have been waiting'))
      .catchError((err) => print(err));

Async Await Syntax

Async-await provides a cleaner (arguably) syntax for writing asynchronous code. The async keyword tells Dart to return a Future, while await pauses the execution of the function until the Future resolves.

file_type_dartlang futures.dart
Future<String> runInTheFuture() async {
  var data = await Future.value('world');

  return 'hello $data';


Ask questions via GitHub below OR chat on Slack #questions