text
stringlengths
1
372
<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 {
final response = await http.get(
uri.parse('https://jsonplaceholder.typicode.com/albums/1'),
);
if (response.statuscode == 200) {
// if the server did return a 200 OK response, then parse the JSON.
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 load album');
}
}
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.');
}
}
class album {
final int id;