Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't use MediaType when I create MultipartFile #2212

Closed
MateusLucasDaSilva opened this issue May 15, 2024 · 1 comment · Fixed by #2205
Closed

I can't use MediaType when I create MultipartFile #2212

MateusLucasDaSilva opened this issue May 15, 2024 · 1 comment · Fixed by #2205
Labels
p: dio Targeting `dio` package s: best practise It's the best way to do something so far

Comments

@MateusLucasDaSilva
Copy link

Package

dio

Version

5.4.3+1

Operating-System

Android, iOS, Web, MacOS, Linux, Windows

Adapter

Default Dio

Output of flutter doctor -v

!] Flutter (Channel stable, 3.19.0, on macOS 14.4.1 23E224 darwin-arm64, locale pt-BR)
    • Flutter version 3.19.0 on channel stable at /Users/usuario/fvm/versions/3.19.0
    ! Warning: `dart` on your path resolves to /opt/homebrew/Cellar/dart/3.2.0/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/usuario/fvm/versions/3.19.0. Consider adding /Users/usuario/fvm/versions/3.19.0/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bae5e49bc2 (3 months ago), 2024-02-13 17:46:18 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/usuario/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = /Users/usuario/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.87.2)
    • VS Code at /Users/usuario/apps/Visual Studio Code.app/Contents
    • Flutter extension version 3.89.20240501

Dart Version

No response

Steps to Reproduce

When using MultipartFile.fromFile does it ask for a contentType parameter that is of type MediaType? but I can't pass the parameter because it is an http_parser parameter but dio does not export the http_parser

Expected Result

Future<FormData?> _convertForFormDataDio(
GenericFormData genericFormData) async {
final FormData formData = FormData();

for (String mediaPath in genericFormData.filePaths) {
  formData.files.add(
    MapEntry<String, MultipartFile>(
      'files',
      await MultipartFile.fromFile(
        mediaPath,
        filename: path.basename(mediaPath),
        contentType: MediaType.parse('.png') ,
        headers: <String, List<String>>{
          'Content-Type': <String>['multipart/form-data'],
        },
      ),
    ),
  );
}

for (String key in genericFormData.fields.keys) {
  formData.fields.add(MapEntry<String, String>(
      key, genericFormData.fields[key].toString()));
}

return formData;

}

Actual Result

Future<FormData?> _convertForFormDataDio(
GenericFormData genericFormData) async {
final FormData formData = FormData();

for (String mediaPath in genericFormData.filePaths) {
  formData.files.add(
    MapEntry<String, MultipartFile>(
      'files',
      await MultipartFile.fromFile(
        mediaPath,
        filename: path.basename(mediaPath),
        contentType: MediaType.parse('.png') ,//Undefined name 'MediaType'. Try correcting the name to one that is defined, or defining the name.
        headers: <String, List<String>>{
          'Content-Type': <String>['multipart/form-data'],
        },
      ),
    ),
  );
}

for (String key in genericFormData.fields.keys) {
  formData.fields.add(MapEntry<String, String>(
      key, genericFormData.fields[key].toString()));
}

return formData;

}

@MateusLucasDaSilva MateusLucasDaSilva added h: need triage This issue needs to be categorized s: bug Something isn't working labels May 15, 2024
@kuhnroyal
Copy link
Member

See #2205 for previous discussion.
@cfug/devs Do we want to revisit this decision?

@AlexV525 AlexV525 added p: dio Targeting `dio` package s: best practise It's the best way to do something so far and removed h: need triage This issue needs to be categorized s: bug Something isn't working labels May 18, 2024
AlexV525 added a commit that referenced this issue May 22, 2024
Resolves #2212

### Additional context and info (if any)

Exports `DioMediaType` as the alias of `MediaType` from the `http_parser` package so users don't have to import the transitive dependency explicitly.

---------

Signed-off-by: Benjamin <bschoe322@gmail.com>
Co-authored-by: Alex Li <github@alexv525.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: dio Targeting `dio` package s: best practise It's the best way to do something so far
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants