- A Future is used to represent a potential value, or error, that will be available at some time in the future. Receivers of a Future can register callbacks that handle the value or error once it is available. For example:
Future<int> future = getFuture();
future.then((value) => handleValue(value))
.catchError((error) => handleError(error));
- If a future doesn’t produce a usable value, then the future’s type is
Future<void>
.
- A future represents the result of an asynchronous operation, and can have two states:
- Uncompleted When you call an asynchronous function, it returns an uncompleted future. That future is waiting for the function’s asynchronous operation to finish or to throw an error.
- Completed If the asynchronous operation succeeds, the future completes with a value. Otherwise it completes with an error.