Delete data on the internet | Flutter
https://flutter.dev/docs/cookbook/networking/delete-data
1. Add the http package 1. Add the http package To install the http package,
add it to the dependencies section of the pubspec.yaml file.
You can find the latest version of the on pub.dev. dependencies: http: <latest_version> Import the http package. import 'package:http/http.dart' as http;2. Delete data on the server 2. Delete data on the server This recipe covers how to delete an album from the using the http.delete() method.
Note that this requires the id of the album that
you want to delete. For this example,
use something you already know, for example id = 1. Future<http.Response> deleteAlbum(String id) async { final http.Response response = await http.delete( Uri.parse('https://jsonplaceholder.typicode.com/albums/$id'), headers: <String, String>{ 'Content-Type': 'application/json; charset=UTF-8', }, ); return response;
} The http.delete() method returns a Future that contains a Response. 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 class contains the data received from a successful
http call. The deleteAlbum() method takes an id argument that
is needed to identify the data to be deleted from the server.3. Update the screen 3. Update the screen In order to check whether the data has been deleted or not,
first fetch the data from the using the http.get() method, and display it in the screen.
(See the recipe for a complete example.)
You should now have a Delete Data button that,
when pressed, calls the deleteAlbum() method. Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text(snapshot.data?.title ?? 'Deleted'), ElevatedButton( child: const Text('Delete Data'), onPressed: () { setState(() { _futureAlbum = deleteAlbum(snapshot.data!.id.toString()); }); }, ), ],
); 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
Once the delete request has been made,
you can return a response from the deleteAlbum()
method to notify our screen that the data has been deleted. Future<Album> deleteAlbum(String id) async { final http.Response response = await http.delete( Uri.parse('https://jsonplaceholder.typicode.com/albums/$id'), headers: <String, String>{ 'Content-Type': 'application/json; charset=UTF-8', }, ); if (response.statusCode == 200) { // If the server did return a 200 OK response, // then parse the JSON. After deleting, // you'll get an empty JSON `{}` response. // Don't return `null`, otherwise `snapshot.hasData` // will always return false on `FutureBuilder`. return Album.fromJson(jsonDecode(response.body)); } else { // If the server did not return a "200 OK response", // then throw an exception. throw Exception('Failed to delete album.'); }
} FutureBuilder() now rebuilds when it receives a response.
Since the response won’t have any data in its body
if the request was 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.
DA: 67 PA: 31 MOZ Rank: 49