如何在flutter中发送多部分请求中的对象列表? [英] How to send List of objects in a multipart request in flutter?

查看:63
本文介绍了如何在flutter中发送多部分请求中的对象列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多部分请求中发送ManageTagModel列表以及其他模型和文件.

I want to send a List of ManageTagModel in a multipart request along with other models and files..

我不确定如何发送此模型列表.

I am not certain of how to send this List of model..

这是我发送不带列表的多部分请求的代码:

This is my code for sending the multipart request without the List:

   var uri = Uri.parse(...);
    final request = http.MultipartRequest('Post', uri);
    request.fields['Id'] = '3';
    request.fields['Name'] = siteModel.name;
    request.fields['MapAddress'] = siteModel.mapAddress;
    request.fields['Country'] = siteModel.country;
    request.fields['City'] = siteModel.city;
    request.fields['CategoryNb'] = siteModel.categoryNb;
    request.fields['UserId'] = userId;
    request.fields['Caption'] = caption;
    for (File i in
    multipleFiles) {

      final mimeTypeData =
      lookupMimeType(i.path, headerBytes: [0xFF, 0xD8]).split('/');
      print("IMAGE: " + i.path);
      // Attach the file in the request
      final file = await http.MultipartFile.fromPath('files', i.path);
      print(mimeTypeData[0] + " mimeTypeData[0]");
      print(mimeTypeData[1] + " mimeTypeData[1]");

      request.files.add(file);

这是我的模特

  import 'dart:convert';

   class ManageTagModel {
   String posX;
 String posY;
 String postOrder;
 String tagger;
 String tagged;

  ManageTagModel(
  {this.posX, this.posY, this.postOrder, this.tagger, this.tagged});

  //Flutter way of creating a constructor
  factory ManageTagModel.fromJson(Map<String, dynamic> json) => ManageTagModel(
  posX: json['PosX'],
  posY: json['PosY'],
  postOrder: json['PostOrder'],
  tagged: json['Tagged'],
  tagger: json['Tagger']);
  Map<String, dynamic> toMap() {
return {
  "PosX": posX,
  "PosY": posY,
  "PostOrder": postOrder,
  "Tagger": tagger,
  "Tagged": tagged
};
}
}

   List<ManageTagModel> fromJson(String jsonData) {
 // Decode json to extract a map
  final data = json.decode(jsonData);
  return List<ManageTagModel>.from(
  data.map((item) => ManageTagModel.fromJson(item)));
   } 

 String toJson(ManageTagModel data) {
 // First we convert the object to a map
 final jsonData = data.toMap();
  // Then we encode the map as a JSON string
 return json.encode(jsonData);
 }

 List encodeToJson(List<ManageTagModel> list) {
 List jsonList = List();
 list.map((item) => jsonList.add(item.toMap())).toList();
 return jsonList;
 }

我的后端c#方法具有参数List

My backend c# method has a parameter List

感谢您的帮助!

推荐答案

我很确定我在这里已经很晚了,您可能已经找到了解决方案.我经历了多个线程,实际上没有找到任何答案,但出于沮丧而发现自己,并自以为是,对于任何其他迷失的人类灵魂,答案实际上仍然不存在.因此,这是我为仍然停留在这里的任何人提供的解决方案,非常直观.

I'm pretty sure I'm quite late here and you might have already found a solution. I have gone through multiple threads and didn't actually find any answers but discovered myself out of frustration and thought to myself that the answer actually is still not out there for any other lost human soul. So here is my solution for anyone still stuck here which is quite intuitive.

您只需要将列表的所有元素添加为请求中的文件"即可.而不是字段".但是,您必须使用 fromString().而不是 fromPath()方法.

You simply have to add all the elements of the list to the request as "files" instead of "fields". But instead of fromPath() method, you have to use fromString().

final request = http.MultipartRequest('Post', uri);
List<String> ManageTagModel = ['xx', 'yy', 'zz'];
for (String item in ManageTagModel) {
    request.files.add(http.MultipartFile.fromString('manage_tag_model', item));
}

这对我有用,我希望对您也有用.

This worked out for me and I hope it works for you too.

这篇关于如何在flutter中发送多部分请求中的对象列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆