api - 颤振:未处理的异常:无效的参数
问题描述
我正在尝试发送两个图像文件列表,但我有一个错误,我不知道为什么会发生这种情况,这是我使用的功能:
Future<dynamic> upload(List<File> imageFile, List<File> backID) async {
var headers =
Provider.of<LoginUserProvider>(context, listen: false).httpHeader;
var uri = Uri.parse(
"xyz");
var request = new http.MultipartRequest("POST", uri);
var requestCopy = new http.MultipartRequest("POST", uri);
request.headers.addAll(headers);
//request.headers.addAll(Environment.requestHeaderMedia);
for (int i = 0; i < uploadedFilesList.length; i++) {
var length = await imageFile[i].length();
var stream =
// ignore: deprecated_member_use
new http.ByteStream(DelegatingStream.typed(imageFile[i].openRead()));
var multipartFile = http.MultipartFile(
'back_id_photos[$i]',
stream,
length,
contentType: MediaType('application', 'x-tar'),
);
var multipartFilePhotos = new http.MultipartFile(
'photos[$i]',
stream,
length,
contentType: MediaType('application', 'x-tar'),
);
request.fields['section_id'] = VillaID.toString();
request.fields['start_date'] = requeststartDate;
request.fields['end_date'] = requestendDate;
request.fields['names'] = _nameController.toString();
request.fields['phones'] = _phoneController.toString();
request.fields['photos'] = multipartFilePhotos.toString();
request.fields['back_id_photos'] = multipartFile.toString();
//contentType: new MediaType('image', 'png'));
request.files.add(multipartFile);
request.files.add(multipartFilePhotos);
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
var streamedResponse = await requestCopy.send();
var responses = await http.Response.fromStream(streamedResponse);
// ignore: unused_local_variable
final responseData = json.decode(responses.body) as Map<String, dynamic>;
print(json.decode(responses.body));
if (response.statusCode == 200 || response.statusCode == 201) {
return true;
}
}
}
并且在
var response = await request.send();
我正好在点(。)之前有错误,这是显示的错误
未处理的异常:无效参数(输入):不能为空任何人都可以告诉我我应该怎么做才能解决这个问题!
解决方案
如下发送您的多部分文件并用作 await http.Multipart
访问文件是异步功能,需要运行承诺。
尝试这个:
await http.MultipartFile(
'back_id_photos[$i]',
stream,
length,
contentType: MediaType('application', 'x-tar'),
);
或者
request.files.add(new http.MultipartFile.fromBytes('file', await File.fromUri("<path/to/file>").readAsBytes(), contentType: new MediaType('image', 'jpeg')))
推荐阅读
- dependency-injection - 无法使用 Crux 编译皇家应用程序:找不到类文件:mx.rpc.AsyncToken
- c++ - 片段着色器不会在 OpenGL GLSL 中创建像光一样的渐变
- spring-cloud-stream - SerializationFailedException: Spring Cloud Stream Kinesis binder with kpl-kcl-enabled:true
- python - 删除某些行包含列表和其他整数/字符串的重复项
- reactjs - 使用 useContext 的子组件中的未定义状态
- python - 从 csv 文件中读取两个 Numpy 数组
- powershell - 无法使用 Invoke-Command 将参数哈希表传递给另一个脚本
- c# - 如何有效地用另一个列表更新一个列表 C#
- javascript - React 组件从函数更改为类 - 状态和状态挂钩
- kubernetes - Kubernetes 问题中的 Artemis 复制