[image_picker] Handle limit: 1 in pickMultiImage and pickMultipleMedia gracefully#11825
[image_picker] Handle limit: 1 in pickMultiImage and pickMultipleMedia gracefully#11825yashas-hm wants to merge 7 commits into
Conversation
…into fix/issue-187347-limit-1-error
There was a problem hiding this comment.
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.
| * 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. |
There was a problem hiding this comment.
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.
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. |
Summary
Fixes flutter/flutter#187347
pickMultiImage(limit: 1)andpickMultipleMedia(limit: 1)previouslythrew:
because
MultiImagePickerOptionsandMediaOptionsin the platforminterface both reject
limit < 2.Fix
When
limit == 1, the facade now delegates to the equivalentsingle-item picker instead of forwarding to the platform:
pickMultiImage(limit: 1)→ delegates topickImage(source: ImageSource.gallery, ...)pickMultipleMedia(limit: 1)→ delegates topickMedia(...)The result is wrapped in a single-element list, or an empty list if the
user cancelled.
limit <= 0still throws as before.Why the facade, not the platform interface
The
limit < 2guard inMultiImagePickerOptionsandMediaOptionsexists 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 ownsthe user-facing API contract and can transparently route
limit: 1tothe well-tested single-item pickers without touching any platform code.
Tests
limit: 1delegates togetImageFromSourcewithImageSource.gallery(pickMultiImage)limit: 1delegates togetMediawithallowMultiple: false(pickMultipleMedia)limit: 0andlimit: -1still throwArgumentErrorPre-Review Checklist
[shared_preferences]///).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-assistbot 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
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