class Album {
final int id;
final String title;
final int userId;
Album({
required this.id,
required this.title,
required this.userId
});
// factory
factory Album.fromJson(Map<String, dynamic> json) {
return Album(id: json['id'] as int,
title: json['title'] as String,
userId: json['userId'] as int);
}
}
// POST with HTTPS
void postJsonDataWithHttps() async {
final transferObj = {
"title": "foo",
"author": "tiny-tank",
"content": "My handle name is `tiny-tank`.",
};
// Request
final String jsonData = json.encode(transferObj);
var https = await HttpClient();
final Uri postUri = Uri.parse("https://jsonplaceholder.typicode.com/posts");
HttpClientRequest httpCliReq = await https.postUrl(postUri);
httpCliReq.headers.set(HttpHeaders.contentTypeHeader, "application/json; charset=utf8");
httpCliReq.write(jsonData);
// Response
HttpClientResponse httpCliRes = await httpCliReq.close();
final String value = await httpCliRes.transfer(utf8.decoder).join();
...
}