Skip to content

[camera] Make Optional.of constructor const#11247

Merged
auto-submit[bot] merged 1 commit intoflutter:mainfrom
cdeil:camera-cleanup-old-optional-ctor
Mar 19, 2026
Merged

[camera] Make Optional.of constructor const#11247
auto-submit[bot] merged 1 commit intoflutter:mainfrom
cdeil:camera-cleanup-old-optional-ctor

Conversation

@cdeil
Copy link
Contributor

@cdeil cdeil commented Mar 15, 2026

Resolves old TODO from 2023 ( 6e09f5b committed by @camsim99 ) by making the Optional.of constructor const and removing the obsolete runtime ArgumentError.checkNotNull guard that was only needed during Dart 2's mixed-mode (unsound null safety) era. Mixed-mode execution was removed in Dart 3.0, so this is dead code. Not a breaking change.

Note that the entire Optional<T> class (and surrounding CameraController) is copy-pasted across 4 files in the camera packages. Would you prefer to move Optional<T> into camera_platform_interface to share it, or replace it entirely e.g. with a private sentinel pattern in copyWith (as the Flutter framework does for ThemeData)?

Pre-Review Checklist

Copy link

@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 makes the Optional.of constructor const and removes an obsolete ArgumentError.checkNotNull guard. This change is applied to the Optional class which is duplicated in four files within the camera packages. The version is updated and a changelog entry is added. A test is also updated to use the new const constructor.

Comment on lines 1051 to +1052
/// Constructs an Optional of the given [value].
///
/// Throws [ArgumentError] if [value] is null.
Optional.of(T value) : _value = value {
// TODO(cbracken): Delete and make this ctor const once mixed-mode
// execution is no longer around.
ArgumentError.checkNotNull(value);
}
const Optional.of(T value) : _value = value;

Choose a reason for hiding this comment

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

medium

As you've noted in the PR description, this Optional<T> class is duplicated across multiple packages. This change is a good example of why that's a maintainability issue, as it has to be applied in four separate places. It would be a great improvement to refactor this in a follow-up PR.

Two potential approaches you mentioned are:

  1. Move Optional<T> to a shared package (e.g., camera_platform_interface).
  2. Replace its usage with a private sentinel pattern, similar to what's done in the Flutter framework for ThemeData.copyWith.

Moving it to a shared package seems like a good path forward. This would also be a good opportunity to audit the implementation for consistency, as there are some subtle differences between the duplicated versions (e.g., in the transform method signature).

Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g left a comment

Choose a reason for hiding this comment

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

LGTM

Note that the entire Optional<T> class (and surrounding CameraController) is copy-pasted across 4 files in the camera packages. Would you prefer to move Optional<T> into camera_platform_interface to share it

We don't currently attempt to de-dup shared example app code between the federated implementation package examples. It's an open area of future work, but much, much bigger in scope than this particular class.

or replace it entirely e.g. with a private sentinel pattern in copyWith (as the Flutter framework does for ThemeData)?

This is public API (unfortunately; I don't think it actually needed to be), so changing it would be a breaking change for the plugin, for very little value.

@stuartmorgan-g stuartmorgan-g added the CICD Run CI/CD label Mar 16, 2026
@stuartmorgan-g
Copy link
Collaborator

@bparrishMines for secondary review.

Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

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

LGTM

@bparrishMines bparrishMines added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 19, 2026
@auto-submit auto-submit bot merged commit c1f1167 into flutter:main Mar 19, 2026
81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD p: camera platform-android platform-ios platform-macos

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants