text stringlengths 1 474 |
|---|
child: const Text('Create Data'), |
), |
], |
); |
} |
FutureBuilder<Album> buildFutureBuilder() { |
return 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>Update data over the internet |
Updating data over the internet is necessary for most apps. |
The http package has got that covered!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. Updating data over the internet using the http package |
This recipe covers how to update an album title to the |
JSONPlaceholder using the http.put() method. |
<code_start>Future<http.Response> updateAlbum(String title) { |
return http.put( |
Uri.parse('https://jsonplaceholder.typicode.com/albums/1'), |
headers: <String, String>{ |
'Content-Type': 'application/json; charset=UTF-8', |
}, |
body: jsonEncode(<String, String>{ |
'title': title, |
}), |
); |
}<code_end> |
The http.put() method returns a Future that contains a Response.<topic_end> |
<topic_start> |
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 |
Now, use the following steps to update the updateAlbum() |
function to return a Future<Album>: |
<code_start>Future<Album> updateAlbum(String title) async { |
final response = await http.put( |
Uri.parse('https://jsonplaceholder.typicode.com/albums/1'), |
headers: <String, String>{ |
'Content-Type': 'application/json; charset=UTF-8', |
}, |
body: jsonEncode(<String, String>{ |
'title': title, |
}), |
); |
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 update album.'); |
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.