我第一次使用斩波器,所以我需要将文件上传到服务器。在这里我不确定是否正确使用该文件,这是我的代码:
@Post(path: '/{residential_community_id}/documents/', headers: {
'Content-Type': ' multipart/form-data;',
'Accept': 'application/json',
})
Future<Response> postDocuments(
@Path('residential_community_id') String residentialCommunityId,
@Body() Map<String, dynamic> body,
@PartFile('file') String file,
);
这是我用 Provider 调用 postDocuments 的函数,当我在控制台中调用它时,我得到'将对象转换为可编码对象失败:'_File' 的实例'
Future _selectFileToUpload() async {
try {
FilePickerResult result = await FilePicker.platform
.pickFiles(allowMultiple: true, type: FileType.any);
if (result != null) {
//selectedFiles.clear();
setState(() {
result.files.forEach((selectedFile) async {
File file = File(selectedFile.path);
Provider.of<ApiService>(context, listen: false).postDocuments(
'$addressId',
{
'documents[attachments_attributes]': jsonEncode(file),
},
'Proba.png');
});
});
} else {
print("User has cancelled the selection");
}
} catch (e) {
print(e);
}
}