Flutter – Making Http Requests

Hi Friends,

Hope you are all well. In this post let’s talk about making http requests from your Flutter apps.

To learn basics of flutter get my course at a discounted price – here.

Other courses on flutter – here. (Affliate link – keeps my site alive by helping me pay for hosting)

Let’s begin.

A screencast of this post.

 

Create a new app. Open up main.dart file and modify it as shown below.

Let’s break this down.

We are importing the http.dart widget aliasing it as http to perform the requests.

Next, we are making a simple get request to a url that will return a json response. I am simply printing that response on the screen. Async and await keywords are used to perform this xhr call.

Now if you run this app on a device and tap on the button shown in the center, then you can see the response shown in the console.

Alright, now how do we extract specific data from this response ? For that we need to decode this response.

Open up main.dart file and modify it as shown below.

We have imported the dart convert library.

Then we are using JSON.decode() to decode the response. Now we can simply extract any field we want using it’s key. For instance to get the first name.

This line will print the first name on the console.

Try running your app now and tap the button. Watch the console and you’ll see that only the first name is printed.

This is how we could make http calls from our app. Now there’s a lot more in this. For instance we could pass in a lot of stuff in the headers. We could make all kinds of http requests (GET, POST, PUT etc,)

Hope this helped you guys.  If you found this helpful, kindly share it with someone else and help them too.

Join our discord community – here.

Thanks for reading. Peace.. :)

Liked it? Take a second to support admin on Patreon!