Async and Await in Dart

Nikhil Sukhani
2 min readAug 18, 2020

To understand the use of async and await keywords, first you need to understand what is Future in Dart. A Future is used to represent a potential answer or error that will be available at some time in the future.

Suppose you want to fetch data from an API. For this, you need to send an HTTP request and wait for the API to respond and then use the data provided by the API in your application. If not wait for the API to respond then there will be a null value which will result in an error.

So, basically, you need to make Dart understand that it needs to wait for a valid answer before executing further code. That is where async and await comes into play.

Example: Synchronous Function
Result of above code
Example: Asynchronous Function
Result of above code

The above example shows that until the function after the await keyword is executed completely, the next function will not be executed.

An async function runs synchronously (executes line after line) until the first await keyword. This means that within an async function body, all synchronous code before the first await keyword executes immediately.

If this article helped you in any way, please react with a clap and help it reach a wider audience 😄

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (2)

Write a response

it's a good explanation

--

Nice ...Finally Understood async function

--