I am trying to send dio form data with files to an api but this error comes up.

Dio dio = Dio(
        BaseOptions(
          baseUrl: baseUrl,
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ${APIVariables.token}',
          },
        ),
      );

FormData formData = FormData.fromMap({
      "id": complaint.id,
      "title": complaint.title,
      "description": complaint.description,
      "complainedPosition": complaint.complainedPosition,
      "deptId": complaint.departmentId,
      "typeId": complaint.typeId,
      "priortyId": complaint.priorityId,
      "files": complaint.files
          .map((file) async => await MultipartFile.fromFile(file.path,
              filename: basename(file.path)))
          .toList(),
    });

Response response = await dio.post(
        'Complaint',
        data: formData,

      ),

how can I send files with options by correct way and show progress that has uploading percentage, and get response after completion?


Solution 1: amir_a14

First import http parser:

import 'package:http_parser/http_parser.dart';

You need add the content type also for your files like this:

  "files": complaint.files
      .map((file) async => await MultipartFile.fromFile(file.path,
          filename: basename(file.path),contentType: MediaType('image', 'jpeg')))

I don't know what is your files type, change MediaType('image', 'jpeg') if your files media type is different