How to send a array in MultipartRequest in flutter?

1,674

Solution 1

'''

request.fields['unit_id[n]'] = "${arrUnitlist[n]}";

or

for(int i = 0; i < arrUnitlist; i++){
  request.fields['unit_id[$i]'] = '${arrUnitlist[i]}';
}

'''

works for me

Solution 2

it is possible just open Multipart defination and change final fields = <String,String> to final fields = <String,dynamic> or any other datatype you want.

Solution 3

You can add your array data of unit_id like this:

var request = new http.MultipartRequest("POST", uri);
request.headers.addAll(headers);
request.fields['property_usage'] = property_use;
request.fields['unit_id[0]'] = "unitlist1";
request.fields['unit_id[1]'] = "unitlist2";
request.fields['unit_id[2]'] = "unitlist3";
var response = await request.send();
Share:
1,674
Marzook
Author by

Marzook

beginner in flutter..enjoying flutter

Updated on December 18, 2022

Comments

  • Marzook
    Marzook over 1 year

    this is i am testing my api using postman as array

    enter image description here

    but how can i send array using MultipartRequest

    var uri = Uri.parse(Constants.BASEURL + Constants.KEYEORD_CREATE_TENANACY);
      Map<String, String> headers = {
        "Accept": "application/json",
        "Authorization": token
        };
       var request = new http.MultipartRequest("POST", uri);
       request.headers.addAll(headers);
       request.fields['property_usage'] = property_use;
       request.fields['unit_id'] = unitlist;               // here i want unit_id to array
       var response = await request.send();
       print("Tenancy Add Result: ${response.statusCode}");
        if (response.statusCode == 200) {
         print(response);
        }
    
    • Taba
      Taba over 2 years
      Did you got any approach to the issue?
  • Marzook
    Marzook almost 4 years
    yes i am sending image to server ! how to solve this issue
  • Elletlar
    Elletlar over 3 years
    Hi Turaab. Thanks for your answer, but it is not very clear. Can you not provide the code? Kind Regards.