8

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

this is my model:

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

My backend c# method has a parameter List

Any help is appreciated!!

2 Answers 2

9

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.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I ended it by sending the list elements one by one through the fields as done above in my question.
In your case, you are sending the very basic form of a list, just a List of Strings, I'm here trying to figure out how to send a List of Map objects..
You can convert your map into a string by doing jsonEncode(yourMap). Then pass this list of string.
0

if the data was not string

for (int item in _userData['roles']) {
    request.files
        .add(http.MultipartFile.fromString('roles', item.toString()));
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.