Capture and Return Asynchronous Values with Futures in Dart

Jermaine Oppong
InstructorJermaine Oppong
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

Futures represent a computation that does not complete immediately. Where a normal function returns a result, an asynchronous function returns a Future that will eventually contain the result. These are similar to Promises in JavaScript. In this lesson, we will learn how to program asynchronously by writing logic to capture values that are returned at a later time.

Instructor: [00:00] Futures allow you to capture a value or error that will be returned at some point in time. These operate in a similar fashion to promises, if you've worked with JavaScript. Here is a basic example of a future object that returns a result at some point.

[00:16] Running this returns a string label that represents a future instance rather than the actual result. This happens because futures work in an asynchronous manner. We need to use the then method to capture the results once the computation is complete.

[00:31] Here is another example that adds a delayed period of time before returning the result. Use the catchError method to handle exceptions.

[00:52] You can also chain your success and error methods. We can chain as many then blocks as possible. To demonstrate that, let's create a variable called showError, which will be used to trigger the catchError method callback. Then let's modify the computed result with this variable.

[01:16] In our first success handler, convert the result to a map object using the jsonDecode method in the Dart convert library. Dart libraries are also feature-rich with functions that return future objects. Here is an example using the HttpRequest getString method to make an API call.

[01:44] This works as expected as long as a future is returned. For asynchronous functions that are not based on the Future API, we can use the Completer class to wrap it within a future context. To demonstrate, let's create a function that accepts a callback function to be triggered once an asynchronous operation is complete.

[02:09] To wrap this function so that a future is returned, we can use the Completer class that comes from the Dart async library. Create a wrapper function with a return type set to future. Create a Completer object and return its future.

[02:24] Before the return statement, run our lookUpVersion operation. In the callback function, we can resolve our future by invoking the complete method on our Completer object.

[02:43] To trigger the catchError method call, use the completeError method. Future-based APIs can also be used with async/await syntax. To do that, let's create a function, marking it with the async keyword before the opening curly bracket. For any function returning a future, prepend the await keyword before invoking it. Let's invoke this function.

[03:15] To handle exceptions, wrap your await calls in a try-catch statement. Then modify lookUpVersion as future to respond with an error. Let's run again. Let's pass this one more time. This concludes the lesson.

~ 5 years ago

Thanks for the explanation Jermaine Oppong. One question, what would it be same as Promise.all with Futures in dart lang?

~ 5 years ago
Jermaine Oppong
Jermaine Opponginstructor
~ 5 years ago

Hey @Hosarsiph, glad I could help. The equivalent would be Future.wait():

var a = Future(() => 5);
var b = Future(() => 6);
var c = Future(() => 7);

void main() async {
  Future.wait([a, b, c])

Learn more here:

You can try the snippet on DartPad

~ 5 years ago

Awesome! Thanks.

Markdown supported.
Become a member to join the discussionEnroll Today