text stringlengths 1 474 |
|---|
3. Convert the http.Response to a custom Dart object |
While it’s easy to make a network request, |
working with a raw Future<http.Response> |
isn’t very convenient. To make your life easier, |
convert the http.Response into a Dart object.<topic_end> |
<topic_start> |
Create an Album class |
First, create an Album class that contains |
the data from the network request. |
It includes a factory constructor that |
creates an Album from JSON.Converting JSON with pattern matching is only one option. |
For more information, see the full article on |
JSON and serialization. |
<code_start>class Album { |
final int id; |
final String title; |
const Album({required this.id, required this.title}); |
factory Album.fromJson(Map<String, dynamic> json) { |
return switch (json) { |
{ |
'id': int id, |
'title': String title, |
} => |
Album( |
id: id, |
title: title, |
), |
_ => throw const FormatException('Failed to load album.'), |
}; |
} |
}<code_end> |
<topic_end> |
<topic_start> |
Convert the http.Response to an Album |
Use the following steps to update the createAlbum() |
function to return a Future<Album>: |
<code_start>Future<Album> createAlbum(String title) async { |
final response = await http.post( |
Uri.parse('https://jsonplaceholder.typicode.com/albums'), |
headers: <String, String>{ |
'Content-Type': 'application/json; charset=UTF-8', |
}, |
body: jsonEncode(<String, String>{ |
'title': title, |
}), |
); |
if (response.statusCode == 201) { |
// If the server did return a 201 CREATED response, |
// then parse the JSON. |
return Album.fromJson(jsonDecode(response.body) as Map<String, dynamic>); |
} else { |
// If the server did not return a 201 CREATED response, |
// then throw an exception. |
throw Exception('Failed to create album.'); |
} |
}<code_end> |
Hooray! Now you’ve got a function that sends the title to a |
server to create an album.<topic_end> |
<topic_start> |
4. Get a title from user input |
Next, create a TextField to enter a title and |
a ElevatedButton to send data to 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 createAlbum() method. |
<code_start>Column( |
mainAxisAlignment: MainAxisAlignment.center, |
children: <Widget>[ |
TextField( |
controller: _controller, |
decoration: const InputDecoration(hintText: 'Enter Title'), |
), |
ElevatedButton( |
onPressed: () { |
setState(() { |
_futureAlbum = createAlbum(_controller.text); |
}); |
}, |
child: const Text('Create Data'), |
), |
], |
)<code_end> |
On pressing the Create Data button, make the network request, |
which sends the data in the TextField to the server |
as a POST request. |
The Future, _futureAlbum, 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 asynchronous 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 createAlbum() function should throw an exception |
even in the case of a “404 Not Found” server response. |
If createAlbum() returns null, then |
CircularProgressIndicator displays indefinitely. |
<code_start>FutureBuilder<Album>( |
future: _futureAlbum, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.