Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 16, 2025

The saveVideo and saveImageWithPath methods' title parameter is nullable in Dart (String?) but used non-null assertion (!!) in Kotlin, causing crashes when null is passed.

Investigation

The title field is required on Android because:

  1. MediaStore metadata: Sets both TITLE and DISPLAY_NAME fields in MediaStore
  2. Directory creation on Android API 28 and below: Used in File(albumDir, title).path.checkDirs() - an empty string would create invalid paths
  3. MIME type detection: Used first to guess content type (with fallbacks)

On iOS, the title is optional and passed as originalFilename to PHAssetResourceCreationOptions.

Solution

  • Added dependency: path: ^1.8.3 for cross-platform path manipulation
  • Dart side (plugin.dart):
    • Trims the title with title?.trim() to normalize whitespace
    • Derives filename using path.basename() when title is null or empty
    • Always passes a non-null title to platform code
    • Applied to both saveVideo and saveImageWithPath methods
  • Kotlin side (PhotoManagerPlugin.kt):
    • Uses !! for title parameter (safe since Dart guarantees non-null value)
    • Uses String? type for optional parameters like desc and relativePath
// Dart side - before
'title': title,  // Can be null

// Dart side - after
final filePath = inputFile.absolute.path;
title = title?.trim();
if (title == null || title.isEmpty) {
  title = path.basename(filePath);
}
'title': title,  // Always non-null
// Kotlin side - before
val title = call.argument<String>("title")!!  // Crashes on null

// Kotlin side - after
val title = call.argument<String>("title")!!  // Safe - Dart guarantees non-null

This approach:

  • Keeps platform-agnostic logic in Dart
  • Ensures proper MediaStore metadata on all Android versions
  • Prevents directory creation issues on Android API 28 and below
  • Maintains backward compatibility when users provide an explicit title
  • Provides cleaner separation of concerns

Fixes #1318

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug report] The parameters for downloading the video have a problem.</issue_title>
<issue_description>### Version

3.7.1

Platforms

Android

Device Model

Android

flutter info

flutter doctor -v
[✓] Flutter (Channel stable, 3.35.4, on macOS 15.6 24G84 darwin-arm64, locale
    zh-Hans-CN) [1,135ms]
    • Flutter version 3.35.4 on channel stable at
      /Users/guoba/fvm/versions/3.35.4
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d693b4b9db (5 weeks ago), 2025-09-16 14:27:41 +0000
    • Engine revision c298091351
    • Dart version 3.9.2
    • DevTools version 2.48.0
    • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop,
      enable-windows-desktop, enable-android, enable-ios, cli-animations,
      enable-lldb-debugging

[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0)
    [2.9s]
    • Android SDK at /Users/guoba/Library/Android/sdk
    • Emulator version 36.1.9.0 (build_id 13823996) (CL:N/A)
    • Platform android-36, build-tools 36.0.0
    • Java binary at: /Applications/Android
      Studio.app/Contents/jbr/Contents/Home/bin/java
      This is the JDK bundled with the latest Android Studio installation on this
      machine.
      To manually set the JDK path, use: `flutter config
      --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.7+-13880790-b1038.58)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.3) [2.1s]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16E140
    • CocoaPods version 1.16.2

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

[✓] Android Studio (version 2025.1) [6ms]
    • 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 21.0.7+-13880790-b1038.58)

[✓] VS Code (version 1.105.1) [5ms]
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.120.0

How to reproduce?

The title parameter of the saveVideo method has a nullability mismatch between the Dart side and the Kotlin side.

Image Image

Logs

Example code (optional)

Contact

No response</issue_description>

Comments on the Issue (you are @copilot in this section)

@fluttercandies-dev **AI Summary:** The `saveVideo` method's `title` parameter has a nullability mismatch between Dart and Kotlin, causing a potential bug on Android platforms.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Nov 16, 2025
Copilot AI changed the title [WIP] Fix parameters for downloading videos on Android Fix nullability mismatch in saveVideo title parameter on Android Nov 16, 2025
Copilot AI requested a review from AlexV525 November 16, 2025 04:18
@AlexV525
Copy link
Member

@copilot Could you investigate if the title field is necessary when saving? I don't want to make mistakes if it's a required field on supported platforms. You need to go deeper

Copy link
Contributor Author

Copilot AI commented Nov 16, 2025

@copilot Could you investigate if the title field is necessary when saving? I don't want to make mistakes if it's a required field on supported platforms. You need to go deeper

I investigated the title field usage across Android and iOS platforms. The title field is necessary on Android:

Critical usage on Android:

  1. Sets MediaStore TITLE and DISPLAY_NAME fields (IDBUtils.kt:475-476)
  2. On Android API 28 and below, creates directory path: File(albumDir, title).path.checkDirs() (IDBUtils.kt:495)
  3. Used for MIME type detection (with fallbacks)

On iOS:

  • Optional - passed as originalFilename to PHAssetResourceCreationOptions
  • Can be nil without issues

Solution implemented:
When title is null or empty, the filename is now derived from the file path using File(filePath).name. This ensures:

  • Proper MediaStore metadata on all Android versions
  • Prevents directory creation issues on Android API 28 and below
  • Maintains compatibility when users explicitly provide a title

Applied to both saveVideo and saveImageWithPath for consistency.

Commit: 8efacef

Copilot AI changed the title Fix nullability mismatch in saveVideo title parameter on Android Fix nullability mismatch in saveVideo and saveImageWithPath title parameter on Android Nov 16, 2025
@AlexV525
Copy link
Member

@copilot Instead of using File, is there another proper way, like using package:path in Dart, to abstract a filename?

Copy link
Contributor Author

Copilot AI commented Nov 17, 2025

@copilot Instead of using File, is there another proper way, like using package:path in Dart, to abstract a filename?

Implemented using package:path in Dart. The filename derivation now happens in the Dart layer using path.basename() instead of using File in Kotlin. This approach:

  • Keeps platform-agnostic logic in Dart
  • Cleaner separation of concerns
  • Simplified Kotlin code (removed File import and conditional logic)
  • Added path: ^1.8.0 dependency to pubspec.yaml

The logic now uses:

final String effectiveTitle = (title?.isNotEmpty ?? false)
    ? title!
    : path.basename(inputFile.absolute.path);

Commit: 140cef4

Copilot AI changed the title Fix nullability mismatch in saveVideo and saveImageWithPath title parameter on Android Fix nullability mismatch in saveVideo and saveImageWithPath title parameter Nov 17, 2025
@AlexV525 AlexV525 force-pushed the copilot/fix-video-download-parameters branch from b0f6ed9 to 394ae57 Compare November 17, 2025 00:28
@AlexV525 AlexV525 marked this pull request as ready for review November 17, 2025 00:35
@github-actions
Copy link

Download apk from here for 9d23bd4

@AlexV525
Copy link
Member

@copilot Update the top comment to reflect all changes in this PR

Copy link
Contributor Author

Copilot AI commented Nov 18, 2025

@copilot Update the top comment to reflect all changes in this PR

The PR description should be updated to reflect the final implementation after the code improvements. Here's the updated version:


Problem

The saveVideo and saveImageWithPath methods' title parameter is nullable in Dart (String?) but used non-null assertion (!!) in Kotlin, causing crashes when null is passed.

Investigation

The title field is required on Android:

  1. MediaStore metadata: Sets both TITLE and DISPLAY_NAME fields
  2. Directory creation on Android API 28 and below: Used in File(albumDir, title).path.checkDirs()
  3. MIME type detection: Used first to guess content type (with fallbacks)

On iOS, the title is optional and passed as originalFilename to PHAssetResourceCreationOptions.

Solution

  • Added dependency: path: ^1.8.3 for cross-platform path manipulation
  • Dart side (plugin.dart):
    • Trims the title with title?.trim()
    • Derives filename using path.basename() when title is null or empty
    • Always passes a non-null title to platform code
  • Kotlin side (PhotoManagerPlugin.kt):
    • Uses !! for title (safe since Dart guarantees non-null)
    • Uses String? for optional parameters like desc and relativePath

Applied to both saveVideo and saveImageWithPath methods.

This ensures proper MediaStore metadata, prevents directory creation issues on Android API 28 and below, and maintains cleaner separation of concerns.

@github-actions
Copy link

Download apk from here for 00195fe

@AlexV525 AlexV525 merged commit 9e7ce5f into main Nov 18, 2025
19 of 20 checks passed
@AlexV525 AlexV525 deleted the copilot/fix-video-download-parameters branch November 18, 2025 00:23
@AlexV525 AlexV525 added this to the 3.8 milestone Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug report] The parameters for downloading the video have a problem.

2 participants