Skip to content

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

Open
yashas-hm wants to merge 7 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 7 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

Copy link
Copy Markdown

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

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 on lines +3 to +5
* Fixes `pickMultiImage(limit: 1)` throwing an `ArgumentError` by delegating
to `pickImage` when `limit` is exactly 1, since the platform interface
requires `limit >= 2` for multi-image selection.
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

The CHANGELOG entry only mentions pickMultiImage(limit: 1), but the PR also fixes pickMultipleMedia(limit: 1). Please update the CHANGELOG to reflect both fixes.

For example:

* Fixes `pickMultiImage(limit: 1)` and `pickMultipleMedia(limit: 1)` throwing an `ArgumentError` by delegating to single-item pickers when `limit` is exactly 1, since the platform interface requires `limit >= 2` for multi-selection.

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

1 participant