Api 颤振:未处理的异常:参数无效
我试图发送两个图像列表文件,我有一个错误,我不知道为什么会发生这种情况 这是我用于此目的的函数:Api 颤振:未处理的异常:参数无效,api,flutter,dart,unhandled-exception,Api,Flutter,Dart,Unhandled Exception,我试图发送两个图像列表文件,我有一个错误,我不知道为什么会发生这种情况 这是我用于此目的的函数: Future<dynamic> upload(List<File> imageFile, List<File> backID) async { var headers = Provider.of<LoginUserProvider>(context, listen: false).httpHeader; var uri
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;
}
}
}
我的错误正好在点(.)之前,这就是显示的错误
未处理的异常:无效参数(输入):不能为null
谁能告诉我该怎么解决这个问题
按如下方式发送您的多部分部件文件,并使用
等待http。多部分因为访问文件是异步功能,需要承诺才能运行
试试这个:
await http.MultipartFile(
'back_id_photos[$i]',
stream,
length,
contentType: MediaType('application', 'x-tar'),
);
或
request.files.add(新建http.MultipartFile.fromBytes('file',wait file.fromUri(“”.readAsBytes()),contentType:new-MediaType('image','jpeg'))
传递的某些参数值为null,请检查相关方法和参数,确保它们没有null值。我建议对添加到请求中的每个数据应用调试点,并检查您为请求发送的内容。send()。我如何检查某些数据是否为null?我做了,但不起作用
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')))