cfug/dio

Error when uploading image using MultipartFile.fromBytes in version 5.4.3+1

KimiyaZargari opened this issue · 1 comments

Package

dio

Version

5.4.3+1

Operating-System

Android, iOS, Web

Adapter

Default Dio

Output of flutter doctor -v

[√] Flutter (Channel stable, 3.22.0, on Microsoft Windows [Version 10.0.22631.3737], locale en-US)
    • Flutter version 3.22.0 on channel stable at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5dcb86f68f (7 weeks ago), 2024-05-09 07:39:20 -0500
    • Engine revision f6344b75dc
    • Dart version 3.4.0
    • DevTools version 2.34.3

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

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at C:\Users\kimiy\AppData\Local\Android\sdk
    • Platform android-34, build-tools 31.0.0
    • ANDROID_HOME = C:\Users\kimiy\AppData\Local\Android\sdk
    • ANDROID_SDK_ROOT = C:\Users\kimiy\AppData\Local\Android\sdk
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • 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 Build Tools 2019 16.11.33)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
    • Visual Studio Build Tools 2019 version 16.11.34407.143
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2023.3)
    • 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 17.0.10+0--11572160)

[√] VS Code (version 1.85.1)
    • VS Code at C:\Users\kimiy\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.90.0

[√] Connected device (4 available)
    • SM A525F (mobile) • RZ8T50QMFDJ • android-arm64  • Android 13 (API 33)
    • Windows (desktop) • windows     • windows-x64    • Microsoft Windows [Version 10.0.22631.3737]
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 125.0.6422.142
    • Edge (web)        • edge        • web-javascript • Microsoft Edge 126.0.2592.68

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

• No issues found!

Dart Version

3.4.0

Steps to Reproduce

  1. Use Dio version 5.4.3+1. (it works fine on version 5.4.1)
  2. Attempt to upload an image using a MultipartFile.fromBytes in a POST request.
  3. The error mentioned below is thrown.

Code Snippet:

Here is the relevant part of my code:

Future<Either<HttpServiceError, EventModel>> createEvent(
  CreateEventModel eventModel,
) async {
  final json = eventModel.toJson()
    ..removeWhere((key, value) => value == null);
  final FormData formData = FormData.fromMap(
    json,
  );
  try {
    formData.files.addAll(
      [
        MapEntry(
          'photo',
          MultipartFile.fromBytes(
            eventModel.photo!,
            filename: eventModel.imageName,
          ),
        ),
      ],
    );

    return right((await _restClient.createEvent(formData)).data);
  } on DioException catch (e) {
    return left(mapHttpExceptionToError(e));
  }
}

Data Model:

@freezed
class CreateEventModel with _$CreateEventModel {
  const factory CreateEventModel({
    required String title,
    required String description,
    required String longitude,
    required String latitude,
    required String? address,
    required double price,
    @JsonKey(includeToJson: false, includeFromJson: false) List<int>? photo,
    @JsonKey(includeToJson: false, includeFromJson: false) String? imageName,
  }) = _CreateEventModel;

  factory CreateEventModel.fromJson(Map<String, dynamic> json) =>
      _$CreateEventModelFromJson(json);
}

Expected Result

The image should be uploaded successfully without throwing any exceptions as it does in version 5.4.1

Actual Result

An exception is thrown: type 'EqualUnmodifiableListView' is not a subtype of type 'Uint8List' of 'value'.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'EqualUnmodifiableListView' is not a subtype of type 'Uint8List' of 'value'
E/flutter ( 4581): #0 _StreamController.add (dart:async/stream_controller.dart:604:14)
E/flutter ( 4581): #1 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter ( 4581): #2 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:365:11)
E/flutter ( 4581): #3 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:297:7)
E/flutter ( 4581): #4 _MultiStreamController.addSync (dart:async/stream_impl.dart:1127:36)
E/flutter ( 4581): #5 new Stream.fromIterable..next (dart:async/stream.dart:375:26)
E/flutter ( 4581): #6 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter ( 4581): #7 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
E/flutter ( 4581):

Duplicate of #2236. Once we release v5.4.0 this should be fixed, before that you can apply those changes locally.