Async Programming in Dart

Vijay R
vijaycreations
Published in
2 min readSep 2, 2020

It is very much important to maintain the states and synchronous behavior of the compiler.

In dart it is possible to maintain these stuffs using the Future, async and await methods.

In this article we shall be discussing regarding these async programming and explaining it using a simple example.

Video Tutorial

Implementation

First let me create a screen that contains a button. On each button press, we shall be calling a function that would update the value of the local variable.

myFunction1

myFunction2

Here, We will be calling myFunction1 method on each button press and this
myFunction1 in turn will be calling myFunction2 .

Consider this myFunction2 will be dealing with the some kind of API calling and related kind of stuffs. so the execution time of myFunction2 is unknown.
In this case we need to make use of Future async to make sure that the compiler waits till the myFunction2 makes a successful request and gets back a response.

In our code as shown above., In order to imitate the same kind of delay that would occur for making an API calling, we have created a custom delay using Future.delayed method (a delay of 5 seconds). So here in order to say the compiler to wait till this code block completes executing, we make use of await keyword.

Thus by making use of these Future, async and await we will be able to now visualize that value variable is changed from 0 to 1 , only after the delay of 5 seconds.

👨‍💻 Get the Complete Source Code 👉🏻: https://github.com/vijayinyoutube/future_async

--

--

Vijay R
vijaycreations

Hai👋 I’m a flutter developer experienced in designing and developing stunning mobile apps. Reach out for freelance projects: calico.takeoff_01@icloud.com