Basic Concept of Asynchronous programming
Concept of Asynchronous Code
A Simple Synchronous Code :
void main() {
// Synchronous Code
print("test1");
for (int i = 0; i < 5; i++) {
print('hello ${i + 1}');
}
testfunction();
print("test3");
}
void testfunction(){
print("test2");
}
Dart is Single Threaded Note: There is a concept called "isolates" in dart which you can use to emulate multithreading
Couple of asynchronous processes :
Event Loop
Futures & Callbacks
"Callbacks" are special types of functions which gets executed when the asynchronous process is done Note: Callbacks are not only used in asynchronous process , they are used in functional programming too .
void main() {
// Futures
print(" Start fetching images");
getData();
print("Loading Images");
}
void getData() {
//callback
Future.delayed(Duration(seconds: 5), () {
// Logic
print(" Image Fetched !!!!!");
});
}

All future start with an uncompleted state . After the asynchronous process is done it either resolves into "Completed with Data" or "Completed with Error"
Last updated
Was this helpful?