text
stringlengths
1
474
}<code_end>
Hooray!
Now you’ve got a function that updates the title of an album.<topic_end>
<topic_start>
4. Get the data from the internet
Get the data from internet before you can update it.
For a complete example, see the Fetch data recipe.
<code_start>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');
}
}<code_end>
Ideally, you will use this method to set
_futureAlbum during initState to fetch
the data from the internet.<topic_end>
<topic_start>
5. Update the existing title from user input
Create a TextField to enter a title and a ElevatedButton
to update the data on server.
Also define a TextEditingController to
read the user input from a TextField.When the ElevatedButton is pressed,
the _futureAlbum is set to the value returned by
updateAlbum() method.
<code_start>Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8),
child: TextField(
controller: _controller,
decoration: const InputDecoration(hintText: 'Enter Title'),
),
),
ElevatedButton(
onPressed: () {
setState(() {
_futureAlbum = updateAlbum(_controller.text);
});
},
child: const Text('Update Data'),
),
],
);<code_end>
On pressing the Update Data button, a network request
sends the data in the TextField to the server as a PUT request.
The _futureAlbum variable is used in the next step.<topic_end>
<topic_start>
5. Display the response on screen
To display the data on screen, use the
FutureBuilder widget.
The FutureBuilder widget comes with Flutter and
makes it easy to work with async data sources.
You must provide two parameters:Note that snapshot.hasData only returns true when
the snapshot contains a non-null data value.
This is why the updateAlbum function should throw an exception
even in the case of a “404 Not Found” server response.
If updateAlbum returns null then
CircularProgressIndicator will display indefinitely.
<code_start>FutureBuilder<Album>(
future: _futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!.title);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return const CircularProgressIndicator();
},
);<code_end>
<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> updateAlbum(String title) async {