O'Reilly logo

Dart: Scalable Application Development by Ivo Balbaert, Sergey Akopkokhyants, Davy Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Error handling with Futures

This recipe shows you how to handle errors comprehensively when working with Futures. The accompanying code future_errors.dart (inside the bin map in the future_errors project) illustrates the different possibilities; however, this is not a real project, so it is not meant to be run as is.

Getting ready

When the function that returns a Future value completes successfully (calls back) signaled in the code by then, a callback function handleValue is executed that receives the value returned. If an error condition took place, the callback handleError handles it. Let's say this function is getFuture(), with Future as the result and a return value of type T, then this becomes equivalent to the following code:

Future<T> future ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required