Article image Advanced Dart Concepts: Asynchronous Programming

4.5. Advanced Dart Concepts: Asynchronous Programming

Page 43 | Listen in audio

Before we dive into the advanced concepts of Dart, it's important to understand that Dart is an object-oriented programming language that is strongly typed and uses a C-style syntax that can be easily understood by JavaScript or Java developers. Dart was created by Google and is used to build mobile, web and desktop apps. One of Dart's most notable features is Flutter, a mobile application development framework created by Google.

Asynchronous programming is one of the advanced concepts in Dart and is crucial for developing Flutter applications. Asynchronous programming is used to perform lengthy operations like file I/O, network operations, database operations without blocking the rest of the code. This is especially useful in mobile apps, where we want to keep the UI responsive while running time-consuming operations in the background.

In Dart, we have three main concepts when it comes to asynchronous programming: Future, async, and await.

Future

A Future is like a promise that we will eventually get a value or an error. When we initiate an asynchronous operation, it returns a Future. We can then add callbacks to the Future to handle the value when it becomes available or handle any errors that may occur.

Here is an example of how we use a Future in Dart:


Future fetchUserOrder() {
  // Simulating a network operation that takes 2 seconds
  return Future.delayed(Duration(seconds: 2), () => 'Grande Latte');
}

void main() {
  print('Searching user request...');
  fetchUserOrder().then((order) {
    print('User order is: $order');
  });
}

In the example above, fetchUserOrder returns a Future that will be completed with the value 'Grande Latte' after 2 seconds. We use the then method of the Future to register a callback that will be called when the Future completes.

Async and Await

Async and await are used to make asynchronous code cleaner and easier to understand. If we mark a function as async, we can use the await keyword inside it to wait for a Future to complete.

Here is an example of how we use async and await in Dart:


Future fetchUserOrder() {
  return Future.delayed(Duration(seconds: 2), () => 'Grande Latte');
}

Future main() async {
  print('Searching user request...');
  var order = await fetchUserOrder();
  print('User order is: $order');
}

In the example above, we've marked the main function as async, which allows us to use the await keyword to wait for fetchUserOrder to complete. This makes the code easier to read as we don't need to use callbacks.

In short, asynchronous programming in Dart is critical to keeping our Flutter apps responsive. Future, async, and await are the main tools Dart gives us to handle asynchronous operations. Understanding these concepts is crucial to becoming a proficient Flutter developer.

Now answer the exercise about the content:

What is the role of asynchronous programming in Dart, especially when developing Flutter apps?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Advanced Dart Concepts: Streams and Blocks

Next page of the Free Ebook:

44Advanced Dart Concepts: Streams and Blocks

4 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text