Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
future

What are Dart futures?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

A future is a valuable construct that allows asynchronous programming in Dart. Asynchronous programming is programming that caters to delayed operations. These delayed operations happen when you need to fetch data over a server, write to a database, or read a file on the system (commonly referred to as I/O operations).

With asynchronous programming, you can tell your computer that it is free to perform other operations while the asynchronous operation completes.

svg viewer
// Function that replicates a slow operation
Future<void> getOrder(){
Future.delayed(
Duration(seconds: 2), () =>
print("Vanilla Shot"));
}
void main() {
getOrder();
//Notice how the following appears on screen first
print("Getting order...");
}
A simple Future implementation

What is a future?

A future is an instance of the Future class. It can have two states: uncompleted or completed.

Uncompleted

When a future is called, but has not returned a value, the future is in the uncompleted state.

Future<String> getOrder(){
Future.delayed(
Duration(seconds: 2), ()
=> 'vanilla shot');
}
void main() {
var order = getOrder();
print("Getting order...");
print(order);
}
When you run this program the result is null because the Future object has not completed.

Completed

If the asynchronous call is successful, the future completes with a value. Otherwise, it completes with an error.

Future<String> getOrder(){
return Future.delayed(
Duration(seconds: 2), ()
=> 'vanilla shot');
}
void main() async {
var order = getOrder();
print("Getting order...");
print(await order);
}
We can make an operation wait for the result.

Read more about getting a program to wait using async/await, here.

a. Completing with a value

If successful, a future of type Future<String> will return a String value. Similarly, Future<T> (a future with any valid type T) will return T.

b. Completing with an error

If unsuccessful, a future will return an error.

// Function that replicates an error
Future<void> getOrder(){
Future.delayed(
Duration(seconds: 2), ()
=> throw Exception('Some Error'));
}
void main() {
getOrder();
print("Getting order...");
}
You'll encounter an error when you run this program. That is expected behaviour.

Read more about asynchronous programming in Dart in the official documentation.

RELATED TAGS

dart
future
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring