The Asynchronous programming: futures, async, await in Flutter

                This Article teaches you how to write asynchronous code using futures and the async and await keywords. 

What is synchronous learning? Definition explained

Synchronicity means doing something at the same time, and learning, it's no different. Synchronous learning refers to a learning event in which a group of participants is engaged in learning at the same time.

What is asynchronous learning? Definition explained

If synchronous learning takes place at the same time, asynchronous learning refers to the oppositeThe instructor, the learner, and other participants are not engaged in the learning process at the same time. There is no real-time interaction with other people.

Why asynchronous code matters

Asynchronous operations let your program complete work while waiting for another operation to finish. Here are some common asynchronous operations:

  • Fetching data over a network.
  • Writing to a database.
  • Reading data from a file.

What is a future?

A future (lowercase “f”) is an instance of the Future (capitalized “F”) class. A future represents the result of an asynchronous operation and can have two states: uncompleted or completed.


Uncompleted

When you call an asynchronous function, it returns an uncompleted future. That future is waiting for the function’s asynchronous operation to finish or to throw an error.


Completed

If the asynchronous operation succeeds, the future completes with a value. Otherwise, it completes with an error


Completing with a value

A future of type Future<T> completes with a value of type T. For example, a future with the type Future<String> produces a string value. If a future doesn’t produce a usable value, then the future’s type is Future<void>.

Completing with an error

If the asynchronous operation performed by the function fails for any reason, the future completes with an error.


Key terms:

  • Future: the Dart Future class.
  • future: an instance of the Dart Future class

Working with futures: async and await

The async and await keywords provide a declarative way to define asynchronous functions and use their results. Remember these two basic guidelines when using async and await:

  • To define an async function, add async before the function body:
  • The await keyword works only in async functions.

Here’s an example that converts main() from a synchronous to asynchronous function.

First, add the async keyword before the function body:

void main() async { ··· }


As the following two examples show, the async and await keywords result in asynchronous code that looks a lot like synchronous code. The only differences are highlighted in the asynchronous example, which—if your window is wide enough—is to the right of the synchronous example.

import 'dart:ffi';
import 'dart:io';

void main() {
performTask();
}

void performTask() {
tast1();
tast2();
tast3();
}

void tast1() {
print("Task 1 complete");
}

void tast2() {
print("Task 2 complete");
}

void tast3() {
print("Task 3 complete");
}
C:/flutter/bin/cache/dart-sdk/bin/dart.exe --enable-asserts C:\Users\Praveen\AndroidStudioProjects\sqfliteex\lib\main.dart
Task 1 complete
Task 2 complete
Task 3 complete

import 'dart:ffi';
import 'dart:io';

void main() {
performTask();
}

void performTask() {
tast1();
String result = tast2();
tast3(result);
}

void tast1() {
print("Task 1 complete");
}

String tast2() {
Duration d = Duration(seconds: 5);
//sleep(d);
String result = "nul";

Future.delayed(
d,
() {
result = "task 2 data";
print("Task 2 complete");
},
);

return result;
}

void tast3(String result) {
print("Task 3 complete ${result}..");
}

lib/main.dart: Warning: Interpreting this as package URI, 'package:sqfliteex/main.dart'.
Task 1 complete
Task 3 complete nul..
Task 2 complete


Example: asynchronous functions

import 'dart:ffi';
import 'dart:io';

void main() {
performTask();
}

Future<void> performTask() async {
tast1();
String result = await tast2();
tast3(result);
}

void tast1() {
print("Task 1 complete");
}

Future<String> tast2() async{
Duration d = Duration(seconds: 5);
//sleep(d);
String result = "nul";

await Future.delayed(
d,
() {
result = "task 2 data";
print("Task 2 complete");
},
);

return result;
}

void tast3(String result) {
print("Task 3 complete ${result}..");
}
C:/flutter/bin/cache/dart-sdk/bin/dart.exe --enable-asserts C:\Users\Praveen\AndroidStudioProjects\sqfliteex\lib\main.dart
lib/main.dart: Warning: Interpreting this as package URI, 'package:sqfliteex/main.dart'.
Task 1 complete

Task 1 is to wait 
Task 2 complete
Task 3 complete task 2 data..





Comments

Popular posts from this blog

Unlocking the Power of OOP: A Beginner's Guide to Objects, Encapsulation, Inheritance, Abstraction, and Polymorphism

HTTP GET Response in Flutter

Building a Flutter Firebase Firestore CRUD App