代码之家  ›  专栏  ›  技术社区  ›  Jaimin Modi

从Flutter中的Head呼叫响应中获取特定Header值

  •  0
  • Jaimin Modi  · 技术社区  · 1 年前

    我正在使用http执行以下Head call:

    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());
      });
    }
    

    我从Header获得的值如下:

    1. 对于打印(value.hedaers.toString()),

    {x-served-by:psm100.akshar-dev.ml,连接:保持活动,上次修改时间:2022年10月13日星期四00:09:35 GMT,接受范围:字节,日期:2021年11月2日星期三10:24:35 GMT,内容长度:69910,etag:“6347573f-11116”,内容类型:application/json,服务器:openresty}

    1. 对于打印(value.headers.values.toString()),

    (psm100.akshar-dev.ml,keep alive,星期四,2022年10月13日00:09:35 GMT,…,application/json,openresty)

    我想要特定的标题值,即 上次修改时间 钥匙 我怎么能得到它?

    1 回复  |  直到 1 年前
        1
  •  2
  •   Jeet Raval    1 年前

    请尝试以下代码

    value.hedaers['last-modified']
    
        2
  •  0
  •   amit.flutter    1 年前

    在得到respose后,您是否解析了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'],
        );
      }
    }
    

    为了自动生成这个类,您可以使用扩展“JSON到DART”或用户 online tool

    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,
            };
        }