How to Use ‘await’ With Multiple Requests; Concurrency in Flutter and Dart(Part 1)

Geno Tech
App Dev Community
Published in
2 min readJul 11, 2021

--

Flutter Knowledge Sharing #42

In this article, we are going to discuss how to run multiple asynchronous tasks at the same time efficiently. I am showing an example using two HTTP requests. When we are running multiple requests at the same time, It basically allows us to get all results more quickly. That’s the main idea you have to take from this article. Concurrency is an advanced topic in programming, not much difficult but more important. I will write more about concurrency in my next articles. You must have a clear idea about these things.

First I will show you the example. And then I will explain how the concurrency has happened. Here I use the https://jsonplaceholder.typicode.com/ as the API. There we can get a post and comments using HTTP requests. In this example, First I load all data which is the post and comments of that post. Don’t think too much about the UI. Because you can change it in your own way. Here we mostly focused on the await keyword and how it worked.

Future _fetchData() async {
setState(() { _showLoading = true; });

var url1 = Uri.parse('http://jsonplaceholder.typicode.com/posts/1');
var response1 = await http.get(url1);

var url2 = Uri.parse('https://jsonplaceholder.typicode.com/comments?postId=1');
var response2 =…

--

--

Geno Tech
App Dev Community

Software Development | Data Science | AI — We write rich & meaningful content on development, technology, digital transformation & life lessons.