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
  • then
  • chaining "then"
  • async , await

Was this helpful?

  1. Dart Fundamentals -3
  2. Asynchronous Programming

async , await , then

PreviousBasic Concept of Asynchronous programmingNextException Handling in asynchronous code

Last updated 4 years ago

Was this helpful?

You might need to import the dart async library if you are working in a local environment , but if you are using dartpad , don't worry about it

//Import dart async library with the following statement if you are
// working in a local environment, add the following line on top of each code

import "dart:async";

then

void main() {
  // Futures
  // then 
  

  print("Start fetching images");

   Future<String>.delayed(Duration(seconds: 5), () {
    // Logic
    print(" Image Fetched !!!!!");
     
    return "Image1, Image2 , ....";
  }).then((value){
     
     print(value+" "+"test");
     
   });
  
  
  print("Loading Images");
}





void main() {
  // Futures
  // then 
  

  print(" Start fetching images");

    var a =  Future<String>.delayed(Duration(seconds: 5), () {
    // Logic
    print(" Image Fetched !!!!!");
     
    return "Image1, Image2 , ....";
  }).then((value){
     
     print(value+" "+"test");
     
   });
  
  
  print(a);
  
  print("Loading Images");
}


void main() {
  // Futures
  // then 
  

  print(" Start fetching images");

   getData().then((value){
     
     
     print(value+" "+"test");
     
     
   });
  
  
  print("Loading Images");
}


Future<String> getData(){
  
      Future<String> a = Future<String>.delayed(Duration(seconds: 5), () {
    // Logic
    print(" Image Fetched !!!!!");
         return "Image1 ,Image 2 ,....";
  
  });
  
  return a;
  
}

chaining "then"


void main() {
  // Futures
  // chaining then

  print(" Start fetching images");

  getData().then((images) {
    return images + " " + "caption";
  }).then((captionedimages) {
    print(captionedimages + " " + " save to disk");
  });

  print("Loading Images");
}

Future<String> getData() {
  Future<String> a = Future<String>.delayed(Duration(seconds: 5), () {
    // Logic
    print(" Image Fetched !!!!!");
    return "Image1 ,Image 2 ,....";
  });

  return a;
}

async , await

void main() async {
  
  // async , await 
  
  
  var images = await fetchImage();
  print(images);
  var capImages = await captionImages(images);
  print(capImages);
  var saved = await savetodisk(capImages);
  print(saved);  
}




Future<String> fetchImage() async {
  await Future<String>.delayed(const Duration(seconds: 5));
  return "Image1,Image2,....";
}


Future<String> captionImages(images) async {
  await Future<String>.delayed(const Duration(seconds: 5));
  return images+" "+"caption";
}


Future<String> savetodisk(capImages) async {
  await Future<String>.delayed(const Duration(seconds: 5));
  return capImages+" "+"Saved";
}



Flutter provides a high level API called "Futurebuilder" which makes dealing with Future much easier