Networking & http in flutter: Deleting Data from the Internet in Flutter
Introduction
In modern applications, the ability to delete data is as crucial as any other CRUD operation. Flutter provides an efficient way to handle data deletion through HTTP requests. In this blog post, we'll guide you through the process of deleting data from a server and reflecting those changes in your Flutter UI.
This recipe covers how to delete data over the internet using the http
package.
This recipe uses the following steps:
Add the
http
package.Delete data on the server.
Update the screen.
Adding the HTTP Package
Before we begin, ensure you have the http
package installed in your Flutter project. If you haven’t added it yet, run the following command:
Then, import the package in your Dart file:
Permissions
Make sure to have the necessary permissions for internet access in your AndroidManifest.xml
file:
With the http
package and permissions in place, you're ready to make HTTP requests.
Deleting Data from the Server
To delete data from a server, you will use the http.delete()
method. For demonstration purposes, we'll use the JSONPlaceholder API to delete an album.
Here’s how you can define a function to perform a delete request:
In this function, we are making a DELETE request to remove the album with the specified ID.
The http.delete()
method returns a Future
that contains a Response
.
Future
is a core Dart class for working with async operations. A Future object represents a potential value or error that will be available at some time in the future.The
http.Response
the class contains the data received from a successful HTTP call.The
deleteAlbum()
method takes anid
argument that is needed to identify the data to be deleted from the server.
Updating the UI After Deletion
After successfully deleting an album, it’s essential to update the UI to reflect this change. To do this, you can manage the state in your Flutter application by using a StatefulWidget
. When an album is deleted, the UI should respond accordingly.
For example, you could fetch the updated list of albums or show a message confirming the deletion. Below is a code snippet that demonstrates how to manage the state after deletion:
Now, when you click on the Delete Data button, the deleteAlbum()
method is called and the id you are passing is the id of the data that you retrieved from the internet. This means you are going to delete the same data that you fetched from the internet.
Returning a response from the deleteAlbum() method
It’s important to handle the response of the delete request to ensure that the deletion was successful. You can modify the deleteAlbum
function to throw an exception if the deletion fails:
FutureBuilder()
now rebuilds when it receives a response. Since the response won't have any data in its body if the request is successful, the Album.fromJson()
method creates an instance of the Album
object with a default value (null
in our case). This behavior can be altered in any way you wish.
That's all! Now you've got a function that deletes the data from the internet.
Complete Example
Now, let's combine all the parts into a complete example that showcases the functionality of deleting data from a server:
In this complete example, we have a simple app that displays a list of albums fetched from the server. Each album has a delete button, and when pressed, it deletes the corresponding album and refreshes the list to reflect the changes.
Conclusion
In this blog post, we covered how to delete data from a server in Flutter and update the UI accordingly. By leveraging the http
package and managing state within a, you can ensure your app remains responsive and user-friendly. Deleting data is a vital part of any application, and with Flutter, it’s straightforward to implement. Happy coding!