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

Error: Converting object to an encodable object failed: Instance of 'FormData' #2380

Closed
ismailSilkron opened this issue Feb 10, 2025 · 0 comments
Labels
h: need triage This issue needs to be categorized s: bug Something isn't working

Comments

@ismailSilkron
Copy link

Package

dio

Version

5.8.0

Operating-System

Android

Adapter

Default Dio

Output of flutter doctor -v

Flutter (Channel stable, 3.27.0, on Microsoft Windows [Version 10.0.19045.5371], locale en-MY)
    • Flutter version 3.27.0 on channel stable at C:\flutter_sdk\flutter_v3.27
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 8495dee1fd (9 weeks ago), 2024-12-10 14:23:39 -0800
    • Engine revision 83bacfc525
    • Dart version 3.6.0
    • DevTools version 2.40.2

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at C:\Users\Ismail\AppData\Local\Android\sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.5.0)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.5.33414.496
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2024.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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 21.0.3+-12282718-b509.11)

[√] VS Code (version 1.97.0)
    • VS Code at C:\Users\Ismail\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.104.0

[√] Connected device (4 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 15 (API 35) (emulator)
    • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.19045.5371]
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 132.0.6834.160
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 132.0.2957.115

[√] Network resources
    • All expected network resources are available.

Dart Version

3.6.0

Steps to Reproduce

I'm using dio package to upload images/files to my server. However, it gives same error every single time.

Below is the example of code-related snippet:

Future<Result<bool>> _uploadFile(File file) async {
  final String fileName = file.path.split('/').last;

  final FormData formData = FormData.fromMap({
    "api_log": MultipartFile.fromBytes(
      File(file.path).readAsBytesSync(),
      filename: fileName,
      contentType: MediaType.parse("text/plain"),
    ),
  });

  final baseOptions = BaseOptions(
    baseUrl: "https://test.com",
    contentType: Headers.multipartFormDataContentType,
    headers: {},
    connectTimeout: const Duration(seconds: 30),
    receiveTimeout: const Duration(seconds: 30),
  );

  final dio = Dio(baseOptions);

  final Options requestOptions = Options(
    method: 'post',
    headers: {},
    contentType: Headers.multipartFormDataContentType,
  );

  dio.httpClientAdapter = IOHttpClientAdapter(
    createHttpClient: () {
      final HttpClient client =
          HttpClient(context: SecurityContext(withTrustedRoots: false));
      client.badCertificateCallback = (cert, host, port) => true;
      return client;
    },
  );

  await dio.request(
    "/upload_file",
    queryParameters: {},
    data: formData,
    options: requestOptions,
    onSendProgress: (received, total) {
      if (total > 0) {
        final progress = (received / total) * 100;
        print('${progress.toStringAsFixed(0)}% uploaded');
      }
    },
    cancelToken: CancelToken(),
  );

  return Result(data: true);
}

Expected Result

Success execute API call with uploaded files/images

Actual Result

Received DioException error

Image

@ismailSilkron ismailSilkron added h: need triage This issue needs to be categorized s: bug Something isn't working labels Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
h: need triage This issue needs to be categorized s: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant