12. Dart - Async & Await
Asynchronous functions run after a delay or timer had run.
Creating a delayed async function:
void function() {
Future.delayed(Duration(seconds: #),(){})
}
// Future has a delayed method that takes in two arguments. Duration and a function argument. The function runs after the duration.
// Duration is a built in class in Dart.
Using await type:
void function() async{
await Future.delayed(Duration(seconds: #),(){})
}
// Specifying the function as an async type and using await will tell function to only resume the next block of line only after the first await finishes.
// can layer many types of await(s) under each other.
Using await type as a variable to get return values:
void function() async{
String text = await Future.delayed(Duration(seconds: #),(){
return "HELLO";
})
}
// making the await function a variable lets us use it in other parts of the code
Comments
Post a Comment