flutterdio

flutter how to upload file with content type in dio


hello i try to upload image with dio package but i have to set contentType for image but i can not set it and get this error when set MediaType ====> The method 'MediaType' isn't defined for the type 'AnalyzeData'.

this is mycode

 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);

how can i fix it i use last version of Dio package


Solution

  • you should define contentType like this with type and subtype:

    contentType: MediaType(type,subType)

    // example MediaType('image','png')

    Also be careful about MediaType it comes from diffrent libraries so you should use package http_parser to use it...