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 opposite. The 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 inasync
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 { ··· }
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");
}
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}..");
}
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}..");
}
Comments
Post a Comment