From 5b03a3834f0b6ca9fbe282b3786c199076152737 Mon Sep 17 00:00:00 2001 From: Camille Simon <43054281+camsim99@users.noreply.github.com> Date: Thu, 2 Nov 2023 21:43:37 +0000 Subject: [PATCH] [camerax] Fix `_getResolutionSelectorFromPreset` NPE (#5287) Fixes a mistake causing a NPE in `_getResolutionSelectorFromPreset`. Essentially, `boundSize` cannot be null except for one case (choosing the maximum available resolution), but the mistake I made was not allowing for that case to be valid. (1/2 fixes really) Fixes https://github.com/flutter/flutter/issues/135293. --- .../camera_android_camerax/CHANGELOG.md | 4 ++++ .../lib/src/android_camera_camerax.dart | 22 ++++++++++--------- .../camera_android_camerax/pubspec.yaml | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/packages/camera/camera_android_camerax/CHANGELOG.md b/packages/camera/camera_android_camerax/CHANGELOG.md index bd39c1cce4f9..fc9c6babd1a9 100644 --- a/packages/camera/camera_android_camerax/CHANGELOG.md +++ b/packages/camera/camera_android_camerax/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.0+22 + +* Fixes `_getResolutionSelectorFromPreset` null pointer error. + ## 0.5.0+21 * Changes fallback resolution strategies for camera use cases to look for a higher resolution if neither the desired diff --git a/packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart b/packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart index 7227bb6cd28b..044778fc20f2 100644 --- a/packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart +++ b/packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart @@ -880,10 +880,14 @@ class AndroidCameraCameraX extends CameraPlatform { break; case ResolutionPreset.max: // Automatically set strategy to choose highest available. - resolutionStrategy = _shouldCreateDetachedObjectForTesting - ? ResolutionStrategy.detachedHighestAvailableStrategy() - : ResolutionStrategy.highestAvailableStrategy(); - break; + if (_shouldCreateDetachedObjectForTesting) { + resolutionStrategy = + ResolutionStrategy.detachedHighestAvailableStrategy(); + return ResolutionSelector.detached( + resolutionStrategy: resolutionStrategy); + } + resolutionStrategy = ResolutionStrategy.highestAvailableStrategy(); + return ResolutionSelector(resolutionStrategy: resolutionStrategy); case null: // If no preset is specified, default to CameraX's default behavior // for each UseCase. @@ -891,17 +895,15 @@ class AndroidCameraCameraX extends CameraPlatform { } if (_shouldCreateDetachedObjectForTesting) { - resolutionStrategy ??= ResolutionStrategy.detached( + resolutionStrategy = ResolutionStrategy.detached( boundSize: boundSize, fallbackRule: fallbackRule); return ResolutionSelector.detached( resolutionStrategy: resolutionStrategy); } - resolutionStrategy ??= - ResolutionStrategy(boundSize: boundSize!, fallbackRule: fallbackRule); - return ResolutionSelector( - resolutionStrategy: ResolutionStrategy( - boundSize: boundSize!, fallbackRule: fallbackRule)); + resolutionStrategy = + ResolutionStrategy(boundSize: boundSize, fallbackRule: fallbackRule); + return ResolutionSelector(resolutionStrategy: resolutionStrategy); } /// Returns the [QualitySelector] that maps to the specified resolution diff --git a/packages/camera/camera_android_camerax/pubspec.yaml b/packages/camera/camera_android_camerax/pubspec.yaml index a7d4c764a890..d7500c09d3ae 100644 --- a/packages/camera/camera_android_camerax/pubspec.yaml +++ b/packages/camera/camera_android_camerax/pubspec.yaml @@ -2,7 +2,7 @@ name: camera_android_camerax description: Android implementation of the camera plugin using the CameraX library. repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22 -version: 0.5.0+21 +version: 0.5.0+22 environment: sdk: ">=2.19.0 <4.0.0"