Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send array in a formdata in Flutter using Dio package?

Tags:

flutter

dart

dio

I want to send a File with a complex JSON object containing JSON Array. How can I do it? enter image description here

I want to send this kind of FormData. Here is how I have implemented it:

final data = {
  "id": 60,
  "first_name": "Amit",
  "last_name": "Sharma",
  "email": "[email protected]",
  "phone_no": "1111111111",
  "addr": "Lko",
  "status": "VERIFIED",
  "total_funds": 0,
  "bankDetails": [
    {"name": "ASD", "acc": "123"},
    {"name": "ASDFG", "acc": "1234"}
  ]
};
if (file != null) {
  data['pic'] =
      MultipartFile.fromFileSync(file.path, filename: 'profile_image');
}
final formData = FormData.fromMap(data);

final formData = FormData.fromMap(data);

final res = await _dio
    .post(
      '$kBaseUrl$kUploadPlanterStory',
      options: Options(
        headers: headers,
        contentType: Headers.formUrlEncodedContentType,
      ),
      data: formData,
    )
    .catchError((e) => throw getFailure(e));

print(res);

}

like image 263
jitendra purohit Avatar asked Oct 19 '25 03:10

jitendra purohit


1 Answers

https://github.com/flutterchina/dio/issues/1155#issuecomment-842492719

Another method would be using the ListFormat.multiCompatible param in FormData.

And I believe this will work on nested arrays without changing the structure of your body, encoding or adding [] to the keys: e.g.

FormData.fromMap(data, ListFormat.multiCompatible); // <-- add this
like image 88
Sunn Avatar answered Oct 20 '25 18:10

Sunn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!