你好,我尝试使用 dio 包上传图像,但我必须为图像设置 contentType 但我无法设置它并在设置 MediaType 时出现此错误 ====> 未为类型“AnalyzeData”定义方法“MediaType” .
这是我的代码
Dio dio = new Dio();
dio.options.headers = {
'token': token,
};
try {
String ip = await getServerIP();
FormData formData = new FormData.fromMap({
"front":front==null?null: MultipartFile.fromFileSync(
front,
filename: "image.png",
contentType: MediaType() <=== get error in this line
),
}
);
response = await dio.post("$url", data: formData);
我该如何解决
我使用最新版本的 Dio 包
最佳答案
您应该使用类型和子类型定义这样的 contentType:
内容类型:媒体类型(类型,子类型)
//示例 MediaType('image','png')
还要注意 MediaType 它来自不同的库,因此您应该使用包 http_parser 来使用它...
关于 flutter 如何在dio中上传内容类型的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61279317/