Intermediate Dart
  • Welcome to the Course
  • Course Overview
  • Resources
  • Dart Fundamentals-2
    • Section Overview
    • Basic Concepts
      • Class
      • Object/Instance
      • Instance /Field Variables
      • Methods
      • "this" keyword-1
    • Constructor
      • Default Constructor
      • Parameterised Constructor
        • Basic Parameterised Constructor & "this" keyword -2
        • Parameterised Constructor with Syntactic sugar
        • Optional Arguments in Parameterised Constructor
      • Named Constructor
    • Getters & Setters and Encapsulation
      • Access Modifiers and _ symbol
      • Implicit / Default Getters & Setters
      • Custom Getters and Setters
      • Encapsulation
    • Inheritance
      • Inheritance, Types of Inheritance , Terminology
      • Method Overriding
      • @override annotation
      • super Keyword -1
      • super Keyword -2
    • Polymorphism
    • Static Variables and Methods
    • Abstract Class and Abstract Methods
    • Interface
    • Miscellaneous concepts
      • Untitled
      • Object Class
      • Mixin
      • Immutable Instances
      • Cascade notation
  • Dart Fundamentals -3
    • Section Overview
    • Asynchronous Programming
      • Basic Concept of Asynchronous programming
      • async , await , then
      • Exception Handling in asynchronous code
    • Functional Programming
      • Anonymous functions
      • High Level Iterators
    • Advanced Asynchronous Programming
      • Isolates
      • Streams
      • Generators
      • Microtask and Zones
    • Method & Constructor Overloading
Powered by GitBook
On this page
  • Concept of Asynchronous Code
  • Event Loop
  • Futures & Callbacks

Was this helpful?

  1. Dart Fundamentals -3
  2. Asynchronous Programming

Basic Concept of Asynchronous programming

PreviousAsynchronous ProgrammingNextasync , await , then

Last updated 4 years ago

Was this helpful?

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 !!!!!");
  });
}

import "dart:html";

void main() {
  // Futures

  print("A");

  var result =
      HttpRequest.getString('https://jsonplaceholder.typicode.com/todos/1');

  print(result);

  print("B");
}

All future start with an uncompleted state . After the asynchronous process is done it either resolves into "Completed with Data" or "Completed with Error"