Skip to content

onSendProgress does not work #2417

@dancb10

Description

@dancb10

Package

dio

Version

5.4.2+1

Operating-System

Android

Adapter

Default Dio

Output of flutter doctor -v

[✓] Flutter (Channel stable, 3.24.1, on macOS 14.6.1 23G93 darwin-arm64, locale en-GB)
    • Flutter version 3.24.1 on channel stable at /Users/danpopescu/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5874a72aa4 (11 months ago), 2024-08-20 16:46:00 -0500
    • Engine revision c9b9d5780d
    • Dart version 3.5.1
    • DevTools version 2.37.2

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/danpopescu/Library/Android/sdk
    • Platform android-34, build-tools 33.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
    • Xcode at /Applications/Xcode-beta.app/Contents/Developer
    • Build 16A5230g
    • CocoaPods version 1.16.2

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

[✓] Android Studio (version 2022.1)
    • 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 11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.101.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.114.0

[✓] Connected device (5 available)
    • SM G973F (mobile)               • RF8M33H660E           • android-arm64  • Android 12 (API 31)
    • SM G973F (mobile)               • RF8M42RS2JZ           • android-arm64  • Android 12 (API 31)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.6.1 23G93 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 14.6.1 23G93 darwin-arm64
    • Chrome (web)                    • chrome                • web-javascript • Google Chrome 138.0.7204.93
    ! Error: Browsing on the local area network for iPhone 13. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for Dan’s iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
    ! Error: Browsing on the local area network for Dan’s Iphone spart. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)

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

• No issues found!

Dart Version

3.5.1

Steps to Reproduce

Future<void> uploadFileToGoogleBucket(
  ChatInputAttachmentSignedUrl signedUrl,
  ChatInputAttachmentRestModel attachment,
  Function(int)? onProgressCallback,
) async {
  File attachmentFile = File(attachment.finalPath!);
  String fileName = "${attachment.hash}${attachment.extension}";

  var fileSize = attachmentFile.lengthSync();
  MultipartFile fileUpload = await MultipartFile.fromFile(attachmentFile.path);

  String base64Hash = "";
  if (attachment.isThumbnail == true) {
    // if this is the movie thumbnail then we use its hash instead of the attachment hash which is for the actual
    // movie file, this is used to verify the file integrity on GCP side
    base64Hash = hexToBase64(attachment.thumbnailHash!);
  } else {
    base64Hash = hexToBase64(attachment.hash!);
  }

  logger.i(
      "ChatModelHelper:uploadFileToGoogleBucket: uploading file to google bucket: ${attachment.finalPath} fileSize: ${fileSize.toString()} fileName: ${fileName}}");

  var headers = {
    'Content-Type': attachment.subtype,
    'x-goog-content-length-range':
        '${fileSize.toString()},${fileSize.toString()}',
    'Content-MD5': base64Hash,
    'Connection': 'keep-alive',
  };

  logger.i(
      "ChatModelHelper:uploadFileToGoogleBucket: using headers: ${headers}}");

  try {
    final dio = Dio();
    var response = await dio.put(
      signedUrl.url!,
      data: attachmentFile.openRead(),
      options: Options(headers: headers),
      onSendProgress: (bytes, total) {
        logger.d(
            "ChatModelHelper:uploadFileToGoogleBucket: progress: ${bytes} total: ${total}");
        if (onProgressCallback != null) {
          // Calculate the progress percentage
          int progress = ((bytes / total) * 100).toInt();
          // Invoke the provided callback with the progress percentage
          onProgressCallback(progress);
        }
      },
    );

    if (response.statusCode == 200) {
      return;
    } else {
      logger.e(
          "ChatModelHelper:uploadFileToGoogleBucket: uploading file to google bucket failed: ${response.statusCode}, ${response.toString()}");
      throw Exception(
          'ChatModelHelper:uploadFileToGoogleBucket: uploading file to google bucket failed: ${response.statusCode}, ${response.toString()}');
    }
  } catch (e) {
    throw Exception(
        'ChatModelHelper:uploadFileToGoogleBucket: uploading file to google bucket failed: ${e.toString()}');
  }
}

this code used to work but the now in dio i've tested also with the latest version this doesn't work anymore. I see multiple people complaining about the same issues https://stackoverflow.com/questions/61635094/flutter-how-to-increase-onsendprogress-calls-for-a-fluid-progress-bar-animation.

Expected Result

Expected this to work

Actual Result

The file upload works but the onSendProgress method is never triggered

Metadata

Metadata

Assignees

No one assigned

    Labels

    h: need triageThis issue needs to be categorizeds: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions