How to send List of objects in a multipart request in flutter?

2,990

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.

Share:
2,990
DCodes
Author by

DCodes

Happy Coding ^^

Updated on November 19, 2022

Comments

  • DCodes
    DCodes over 1 year

    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!!