Myself and the rest of Team Tiger (from the Flutter dev internship program we’ve recently joined) have just been handed a challenge to work with a simple API, to send HTTP requests and receive responses which a mobile app should understand. I’ve always been fond of reading and understanding web network requests, and knew the challenge would be fun.
If we want to work with HTTP requests, we need to import several dependencies:
The dart packages are built into Flutter, but we also need an external http package, which we have to include in our pubspec.yaml file, like so:
And now we can try sending post requests through our mobile app! Here’s an example code of asynchronously deleting something through an API:
The code is straightforward, sending a .post() method containing the URL of where we want to perform an activity and a body field which tells us what information we need to send to said URL, all inside an asynchronous Future. We specify that we want to close the HTTP client after the POST request gets completed by sending a client.close command inside the .whenComplete() method.
We can change the app state by running desired commands inside the setState() method. In the example code above, we’re just printing the response received from the POST request. What you want to do about that response is up to you. You can also return the response to someplace in the app if you want.
The if (!mounted) clause is there just to guard us from situations where the user suddenly moves to another part of the application while the POST request is still running. In that case, the request immediately stops and sends a desired return.












