Search code examples
flutterflutter-webform-datadio

Upload image flutter web with dio formdata


I am trying using file_picker and dio packages to upload files as form data.

This is for flutter web and it seems MultipartFile.fromFile is not accepted.

What I tried is the following:

if (result != null) {    
  for (var file in result.files) {
    final formData = FormData.fromMap({
      ...someOtherData,
      'file': File(file.name), // <------ I guess this is where the issue is, I also tried file instead of File(file.name)
    });
    
    dio.post(
      url,
      data: formData,
    );
  }
}

Solution

  • Ok, I found it, leaving here for someone having the same problem

    if (result != null) {    
      for (var file in result.files) {
        final formData = FormData.fromMap({
          ...someOtherData,
          'file': MultipartFile.fromBytes(file.bytes as List<int>)
        });
        
        dio.post(
          url,
          data: formData,
        );
      }
    }