Skip to content

[image_picker] Handle limit: 1 in pickMultiImage and pickMultipleMedia gracefully#11825

Open
yashas-hm wants to merge 11 commits into
flutter:mainfrom
yashas-hm-os:fix/issue-187347-limit-1-error
Open

[image_picker] Handle limit: 1 in pickMultiImage and pickMultipleMedia gracefully#11825
yashas-hm wants to merge 11 commits into
flutter:mainfrom
yashas-hm-os:fix/issue-187347-limit-1-error

Conversation

@yashas-hm

Copy link
Copy Markdown

Summary

Fixes flutter/flutter#187347

pickMultiImage(limit: 1) and pickMultipleMedia(limit: 1) previously
threw:

  Invalid argument (limit): cannot be lower than 2: 1

because MultiImagePickerOptions and MediaOptions in the platform
interface both reject limit < 2.

Fix

When limit == 1, the facade now delegates to the equivalent
single-item picker instead of forwarding to the platform:

  • pickMultiImage(limit: 1) → delegates to pickImage(source: ImageSource.gallery, ...)
  • pickMultipleMedia(limit: 1) → delegates to pickMedia(...)

The result is wrapped in a single-element list, or an empty list if the
user cancelled. limit <= 0 still throws as before.

Why the facade, not the platform interface

The limit < 2 guard in MultiImagePickerOptions and MediaOptions
exists because the underlying platform multi-picker APIs genuinely do not
support selecting exactly one item, they require at least two. Relaxing
the validation in the platform interface would push the burden onto every
platform implementation to handle a case their native API may not
support, risking silent misbehaviour on some platforms.

The facade (image_picker) is the right place for this: it already owns
the user-facing API contract and can transparently route limit: 1 to
the well-tested single-item pickers without touching any platform code.

Tests

  • limit: 1 delegates to getImageFromSource with ImageSource.gallery (pickMultiImage)
  • limit: 1 delegates to getMedia with allowMultiple: false (pickMultipleMedia)
  • Both return a single-element list when an item is picked, empty list when cancelled
  • limit: 0 and limit: -1 still throw ArgumentError
  • All existing tests continue to pass

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Closes flutter/flutter#187347

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates ImagePicker to delegate to single-item pickers (pickImage and pickMedia) when pickMultiImage or pickMultipleMedia is called with a limit of 1, preventing an ArgumentError since multi-image selection requires a limit of at least 2. The changes include corresponding unit tests and a CHANGELOG update. The reviewer recommends updating the CHANGELOG entry to explicitly mention the fix for pickMultipleMedia alongside pickMultiImage.

Comment thread packages/image_picker/image_picker/CHANGELOG.md Outdated
@stuartmorgan-g

Copy link
Copy Markdown
Collaborator

[x] I followed the version and CHANGELOG instructions, using semantic versioning and the repository CHANGELOG style, or I have commented below to indicate which documented exception this PR falls under1.

Thanks for the contribution! You’ve checked boxes in the PR checklist above that are not reflected in this PR, so I’m assuming this is a work in progress and am marking it as a Draft. Please review the checklist, updating the PR as appropriate, and when the state of the PR as posted reflects the checklist please feel free to mark it as ready for review.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft June 7, 2026 15:31
@yashas-hm

Copy link
Copy Markdown
Author

@stuartmorgan-g Thank you for the review! I've bumped the version in pubspec.yaml to 1.2.3 to reflect the bug fix. The ## NEXT section in CHANGELOG.md is intentional, I followed the repository convention of leaving the placeholder for maintainers to replace at release time. Please let me know if anything else needs to be addressed.

@yashas-hm yashas-hm marked this pull request as ready for review June 7, 2026 19:28

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates image_picker to delegate to single-item pickers (pickImage and pickMedia) when limit is set to 1 in pickMultiImage and pickMultipleMedia, preventing ArgumentError exceptions. It also updates the corresponding tests and bumps the package version. Feedback suggests refactoring the raw futures and ternary operators in the new delegation logic to use async/await and collection if for better alignment with Dart idioms.

I am having trouble creating individual review comments. Click here to see my feedback.

packages/image_picker/image_picker/lib/image_picker.dart (135-146)

medium

According to Effective Dart, it is preferred to use async/await over raw futures and .then(). Additionally, using collection if is more idiomatic in Dart than ternary operators for conditional list elements.

  }) async {
    // limit: 1 would fail MultiImagePickerOptions validation (requires >= 2),
    // so delegate to pickImage which already handles single-image selection.
    if (limit == 1) {
      final XFile? image = await pickImage(
        source: ImageSource.gallery,
        maxWidth: maxWidth,
        maxHeight: maxHeight,
        imageQuality: imageQuality,
        requestFullMetadata: requestFullMetadata,
      );
      return <XFile>[if (image != null) image];
    }
References
  1. Effective Dart: Prefer async/await over using raw futures, and prefer using collection literals with if rather than conditional operators when building collections. (link)

packages/image_picker/image_picker/lib/image_picker.dart (263-273)

medium

According to Effective Dart, it is preferred to use async/await over raw futures and .then(). Additionally, using collection if is more idiomatic in Dart than ternary operators for conditional list elements.

  }) async {
    // limit: 1 would fail MediaOptions validation (requires >= 2),
    // so delegate to pickMedia which already handles single-item selection.
    if (limit == 1) {
      final XFile? media = await pickMedia(
        maxWidth: maxWidth,
        maxHeight: maxHeight,
        imageQuality: imageQuality,
        requestFullMetadata: requestFullMetadata,
      );
      return <XFile>[if (media != null) media];
    }
References
  1. Effective Dart: Prefer async/await over using raw futures, and prefer using collection literals with if rather than conditional operators when building collections. (link)

@stuartmorgan-g

Copy link
Copy Markdown
Collaborator

I followed the repository convention of leaving the placeholder for maintainers to replace at release time

Could you let me know which specific part of the documentation led you believe that you should not change the version in the changelog, so that we can clarify that?

@yashas-hm

Copy link
Copy Markdown
Author

I followed the repository convention of leaving the placeholder for maintainers to replace at release time

Could you let me know which specific part of the documentation led you believe that you should not change the version in the changelog, so that we can clarify that?

Apologies, I got confused between the require version change and doesn't require version change. Updated the Changelog.

@stuartmorgan-g

Copy link
Copy Markdown
Collaborator

Apologies, I got confused between the require version change and doesn't require version change.

This doesn't answer my question.

Also, are you saying that you previously thought that the PR was version exempt, but then changed the version number in the pubspec.yaml anyway?

As I asked above, please provide the specific parts of the docs that led you to these conclusions.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft June 8, 2026 12:26
@yashas-hm

Copy link
Copy Markdown
Author

I'm a first-time contributor to the repo and misread the documentation. There was no specific part that explicitly said to leave ## NEXT; it was an error in my understanding of the wording. The changelog and version have since been updated correctly. Sorry for the unclear responses!

The PR is not version exempt as it fixes a bug that needs to be published on pub.dev

@stuartmorgan-g

Copy link
Copy Markdown
Collaborator

it was an error in my understanding of the wording

I understand that. My goal is to find out what wording you misunderstood, because without knowing what wording is hard to understand, it's very hard to improve that wording.

Yesterday, you read the documentation and concluded that there was a "repository convention of leaving the placeholder for maintainers to replace at release time". That's the opposite of what we want the docs are intended to convey, so clearly something in the docs is unclear. It would be very helpful if you could explain what unclear wording led to that conclusion.

@stuartmorgan-g stuartmorgan-g marked this pull request as ready for review June 8, 2026 15:26
@stuartmorgan-g stuartmorgan-g requested a review from tarrinneal June 8, 2026 15:26

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates pickMultiImage and pickMultipleMedia in image_picker.dart to delegate to single-item pickers when limit is exactly 1, preventing validation errors from the platform interface. It also updates the package version to 1.2.3 and adds corresponding unit tests. The review feedback suggests explicitly validating that limit is at least 1 in both methods to avoid misleading error messages when a limit less than 1 is provided.

Comment on lines +133 to +135
// limit: 1 would fail MultiImagePickerOptions validation (requires >= 2),
// so delegate to pickImage which already handles single-image selection.
if (limit == 1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When limit is less than 1 (e.g., 0 or negative), the validation falls through to MultiImagePickerOptions.createAndValidate, which throws an ArgumentError stating that the limit cannot be lower than 2. Since limit: 1 is now supported, this error message is misleading. Explicitly validating that limit is at least 1 in the facade provides a more accurate error message.

Suggested change
// limit: 1 would fail MultiImagePickerOptions validation (requires >= 2),
// so delegate to pickImage which already handles single-image selection.
if (limit == 1) {
if (limit != null && limit < 1) {
throw ArgumentError.value(limit, 'limit', 'cannot be lower than 1');
}
// limit: 1 would fail MultiImagePickerOptions validation (requires >= 2),
// so delegate to pickImage which already handles single-image selection.
if (limit == 1) {

Comment on lines +259 to +261
// limit: 1 would fail MediaOptions validation (requires >= 2),
// so delegate to pickMedia which already handles single-item selection.
if (limit == 1) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When limit is less than 1 (e.g., 0 or negative), the validation falls through to MediaOptions.createAndValidate, which throws an ArgumentError stating that the limit cannot be lower than 2. Since limit: 1 is now supported, this error message is misleading. Explicitly validating that limit is at least 1 in the facade provides a more accurate error message.

Suggested change
// limit: 1 would fail MediaOptions validation (requires >= 2),
// so delegate to pickMedia which already handles single-item selection.
if (limit == 1) {
if (limit != null && limit < 1) {
throw ArgumentError.value(limit, 'limit', 'cannot be lower than 1');
}
// limit: 1 would fail MediaOptions validation (requires >= 2),
// so delegate to pickMedia which already handles single-item selection.
if (limit == 1) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[image_picker] Do not error if a limit of 1 is passed to pickMultiImage

2 participants