Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter how to upload file with content type in dio

Tags:

flutter

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

like image 299
mohammad shabani Avatar asked Apr 17 '20 19:04

mohammad shabani


1 Answers

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...

like image 142
SSDN Avatar answered Sep 20 '22 21:09

SSDN