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 ...
Get Dart: Scalable Application Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.