颤动如何在dio中上传内容类型的文件

moh*_*ani 7 flutter dio

你好,我尝试使用 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);
Run Code Online (Sandbox Code Playgroud)

我如何修复它我使用的是最新版本的 Dio 包

SSD*_*SDN 15

您应该使用类型和子类型定义这样的 contentType:

内容类型:媒体类型(类型,子类型)

// 示例 MediaType('image','png')

还要注意 MediaType 它来自不同的库,因此您应该使用包 http_parser 来使用它...


Rit*_*mar 9

导入给定的包

import 'package:http_parser/http_parser.dart';
Run Code Online (Sandbox Code Playgroud)

它对我有用


Dee*_*yan 8

只需添加LIB http_parser到使用MediaType()Dio