Async Programming in Dart
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 thismyFunction1
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
Other articles you may like.,
🟡 Change App Launcher Icon in Flutter
🔴 AnimatedContainer Widget in Flutter
Flutter Tutorials
If you want to know more about Flutter and various Widgets in Flutter…?🤓 Then visit my channel vijaycreations🚩
Thanks.,