8

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

mohammad shabani
  • 359
  • 1
  • 8
  • 15

3 Answers3

20

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

SSDN
  • 276
  • 3
  • 8
8

Just Add lib http_parser to use MediaType() with Dio

Deepak Ror
  • 2,084
  • 2
  • 20
  • 26
5

import the given package

import 'package:http_parser/http_parser.dart';

it has worked for me

Ritunjay kumar
  • 261
  • 3
  • 6