text
stringlengths
1
372
void initState() {
super.initState();
_futureAlbum = fetchAlbum();
}
@override
widget build(BuildContext context) {
return MaterialApp(
title: 'update data example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
),
home: scaffold(
appBar: AppBar(
title: const Text('Update data example'),
),
body: container(
alignment: alignment.center,
padding: const EdgeInsets.all(8),
child: FutureBuilder<Album>(
future: _futureAlbum,
builder: (context, snapshot) {
if (snapshot.connectionstate == ConnectionState.done) {
if (snapshot.hasdata) {
return column(
mainAxisAlignment: MainAxisAlignment.center,
children: <widget>[
text(snapshot.data!.title),
TextField(
controller: _controller,
decoration: const InputDecoration(
hintText: 'enter title',
),
),
ElevatedButton(
onPressed: () {
setState(() {
_futureAlbum = updateAlbum(_controller.text);
});
},
child: const Text('Update data'),
),
],
);
} else if (snapshot.haserror) {
return text('${snapshot.error}');
}
}
return const CircularProgressIndicator();
},
),
),
),
);
}
}
<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.