I want to send a File with a complex JSON object containing JSON Array. How can I do it?
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);
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With