Asynchrony in Dart
In this lesson, you will learn approaches for using asynchronous programming in Dart.
We'll cover the following
What is Asynchrony?
Asynchronicity allows multiple things to happen at the same time while synchronicity allows things to happen one at a time.
Asynchrony is the state of not being in synchronization. It is the occurrence of events independent of the main program flow, and ways to handle such events.
Asynchronous operations are used to perform time-consuming operations. Asynchronous operations do not block the thread but allow for their processing to finish at a later time.
The Dart language provides two ways to deal with events/requests asynchronously.
Dart’s dart:async
library provides support for asynchronous programming with the help of Future
and Stream
classes.
-
Future objects: The Futures are objects that represent the results of asynchronous operations. It is like a promise for a result to arrive in the future at some point.
-
Stream objects: A Stream object provides a sequence of events that are either a value or an error.
Future Object
Asynchronous operations results are returned as Future
objects.
Functions that perform expensive work should use the asynchronous model for executing their work. The future object is represented as Future<T>
, where T is the type of results returned from the expensive operation.
When you need the result of a completed Future, you have two options:
- Using
await
andasync
- Using the
Future
API.
In upcoming lessons, we will explore these two options in detail.
Get hands-on with 1400+ tech skills courses.