text
stringlengths
1
474
),
),
),
);
}
}<code_end>
<topic_end>
<topic_start>Delete data on the internet
This recipe covers how to delete data over
the internet using the http package.This recipe uses the following steps:<topic_end>
<topic_start>
1. Add the http package
To add the http package as a dependency,
run flutter pub add:Import the http package.
<code_start>import 'package:http/http.dart' as http;<code_end>
<topic_end>
<topic_start>
2. Delete data on the server
This recipe covers how to delete an album from the
JSONPlaceholder 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.
<code_start>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;
}<code_end>
The http.delete() method returns a Future that contains a Response.<topic_end>
<topic_start>
3. Update the screen
In order to check whether the data has been deleted or not,
first fetch the data from the JSONPlaceholder
using the http.get() method, and display it in the screen.
(See the Fetch Data recipe for a complete example.)
You should now have a Delete Data button that,
when pressed, calls the deleteAlbum() method.
<code_start>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());
});
},
),
],
);<code_end>
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.<topic_end>
<topic_start>
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.
<code_start>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) as Map<String, dynamic>);
} else {
// If the server did not return a "200 OK response",
// then throw an exception.
throw Exception('Failed to delete album.');
}
}<code_end>
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.<topic_end>
<topic_start>
Complete example
<code_start>import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<Album> fetchAlbum() async {