flutter - 如何使用 Dio 包在 Flutter 中的 formdata 中发送数组?
问题描述
我想发送一个包含 JSON 数组的复杂 JSON 对象的文件。我该怎么做?
我想发送这种 FormData。这是我实现它的方式:
final data = {
"id": 60,
"first_name": "Amit",
"last_name": "Sharma",
"email": "j.purohit198@gmail.com",
"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);
}
解决方案
请记住在键后添加“[]”。以表单数据发送数组的最简单方法:
FormData formData = new FormData.fromMap({
"name": "Max",
"location": "Paris",
"age": 21,
"image[]": [1,2,3],
});
Response response = await dio.post(
//"/upload",
"http://143.110.244.110/radius/frontuser/eventsubmitbutton",
data: formData,
onSendProgress: (received, total) {
if (total != -1) {
print((received / total * 100).toStringAsFixed(0) + '%');
}
},
);
推荐阅读
- android - Xamarin.Android 设计器是阿拉伯语的 VS 错误
- php - 上传文件 PHP EC2 不工作没有错误
- ruby - 使用 Ruby gets.chomp 方法向用户询问多个问题
- type-theory - 分离函数的联合类型?
- google-sheets - 在 Google 表格中查找奇数计数的公式
- r - 检查字符是否在数据框中
- android - 将值从单个传递到可完成以产生 rxjava Android
- javascript - 如何使用 webpack 生成 d.ts 和 d.ts.map 文件?
- c++ - 生成从 1 到 1000 的一系列字符的程序,其中包括数字和字母
- cordova - 在离子项目中运行 NPM 安装时出错