我正在使用http执行以下呼叫:
Future<void> fetchModifiedDate(String fileUrl,BuildContext context) async{
if (await checkNetworkConnection(context)) {
var responseValue = [];
var response = http.head(Uri.parse(fileUrl));
response.then((value) {
responseValue = value.headers.values.toString().split(",");
modifiedDate = responseValue[1].trim();
print(value.headers.toString());
print(value.headers.values.toString());
});
}我从标题中得到的值如下:
{x-by: psm100.akshar-dev.ml,连接:保持活动,最后修改:清华,2022年10月13日:09:35 GMT,接受范围:字节,日期:10,011月2日10:24:35 GMT,内容长度: 69910,etag:"6347573f-11116",内容类型: application/json,server: openresty}
(psm100.akshar-dev.ml,保持活力,清华,2022年10月13日:09:35格林尼治时间,.,应用/json,开放)
我想要特定的头值,即last-modified键的值。我怎么才能拿到呢?
发布于 2022-11-02 10:42:10
尝试以下代码
value.hedaers['last-modified']发布于 2022-11-02 10:45:27
你有在得到答复后解析json吗?
你能做到的。
if (response.statusCode == 200) {
return Album.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}ALbum是类,您可以根据响应来定义它
class Album {
final int userId;
final int id;
final String title;
const Album({
required this.userId,
required this.id,
required this.title,
});
factory Album.fromJson(Map<String, dynamic> json) {
return Album(
userId: json['userId'],
id: json['id'],
title: json['title'],
);
}
}要自动生成这个类的yupe,可以使用扩展名"JSON to DART“或用户在线工具
class Response {
Response({
this.xServedBy,
this.connection,
this.lastModified,
this.acceptRanges,
this.date,
this.contentLength,
this.etag,
this.contentType,
this.server,
});
String xServedBy;
String connection;
String lastModified;
String acceptRanges;
String date;
int contentLength;
String etag;
String contentType;
String server;
factory Response.fromJson(Map<String, dynamic> json) => Response(
xServedBy: json["x-served-by"],
connection: json["connection"],
lastModified: json["last-modified"],
acceptRanges: json["accept-ranges"],
date: json["date"],
contentLength: json["content-length"],
etag: json["etag"],
contentType: json["content-type"],
server: json["server"],
);
Map<String, dynamic> toJson() => {
"x-served-by": xServedBy,
"connection": connection,
"last-modified": lastModified,
"accept-ranges": acceptRanges,
"date": date,
"content-length": contentLength,
"etag": etag,
"content-type": contentType,
"server": server,
};
}https://stackoverflow.com/questions/74287736
复制相似问题