[camerax] Fixes `unawaited_futures` violations (#4337)
Fixes `unawaited_futures` violations in `camera_android_camerax` plugin. The only `await`s that I did not add are those related to closing/disposing native objects that shouldn't require us to wait for completion.
Part of https://github.com/flutter/flutter/issues/127323.
diff --git a/packages/camera/camera_android_camerax/CHANGELOG.md b/packages/camera/camera_android_camerax/CHANGELOG.md
index 2980f18..e346f78 100644
--- a/packages/camera/camera_android_camerax/CHANGELOG.md
+++ b/packages/camera/camera_android_camerax/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.5.0+8
+
+* Fixes unawaited_futures violations.
+
## 0.5.0+7
* Updates Guava version to 32.0.1.
diff --git a/packages/camera/camera_android_camerax/analysis_options.yaml b/packages/camera/camera_android_camerax/analysis_options.yaml
deleted file mode 100644
index 7c19fab..0000000
--- a/packages/camera/camera_android_camerax/analysis_options.yaml
+++ /dev/null
@@ -1,8 +0,0 @@
-# TODO(stuartmorgan): Remove this file and fix all the unawaited_futures
-# violations. See https://github.com/flutter/flutter/issues/127323
-
-include: ../../../analysis_options.yaml
-
-linter:
- rules:
- unawaited_futures: false
diff --git a/packages/camera/camera_android_camerax/example/lib/camera_controller.dart b/packages/camera/camera_android_camerax/example/lib/camera_controller.dart
index b1b5e9d..d43eb13 100644
--- a/packages/camera/camera_android_camerax/example/lib/camera_controller.dart
+++ b/packages/camera/camera_android_camerax/example/lib/camera_controller.dart
@@ -526,7 +526,7 @@
}
if (value.isStreamingImages) {
- stopImageStream();
+ unawaited(stopImageStream());
}
try {
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 959f35b..aa5d1e0 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
@@ -317,9 +317,9 @@
@override
Future<void> dispose(int cameraId) async {
preview?.releaseFlutterSurfaceTexture();
- liveCameraState?.removeObservers();
+ unawaited(liveCameraState?.removeObservers());
processCameraProvider?.unbindAll();
- imageAnalysis?.clearAnalyzer();
+ unawaited(imageAnalysis?.clearAnalyzer());
}
/// The camera has been initialized.
@@ -423,8 +423,8 @@
/// [cameraId] not used.
@override
Future<void> pausePreview(int cameraId) async {
- _unbindUseCaseFromLifecycle(preview!);
_previewIsPaused = true;
+ await _unbindUseCaseFromLifecycle(preview!);
}
/// Resume the paused preview for the selected camera.
@@ -432,8 +432,8 @@
/// [cameraId] not used.
@override
Future<void> resumePreview(int cameraId) async {
- await _bindPreviewToLifecycle(cameraId);
_previewIsPaused = false;
+ await _bindPreviewToLifecycle(cameraId);
}
/// Returns a widget showing a live camera preview.
@@ -507,7 +507,7 @@
if (videoOutputPath == null) {
// Stop the current active recording as we will be unable to complete it
// in this error case.
- recording!.close();
+ unawaited(recording!.close());
recording = null;
pendingRecording = null;
throw CameraException(
@@ -516,7 +516,7 @@
'while reporting success. The platform should always '
'return a valid path or report an error.');
}
- recording!.close();
+ unawaited(recording!.close());
recording = null;
pendingRecording = null;
return XFile(videoOutputPath!);
@@ -526,7 +526,7 @@
@override
Future<void> pauseVideoRecording(int cameraId) async {
if (recording != null) {
- recording!.pause();
+ await recording!.pause();
}
}
@@ -534,7 +534,7 @@
@override
Future<void> resumeVideoRecording(int cameraId) async {
if (recording != null) {
- recording!.resume();
+ await recording!.resume();
}
}
@@ -614,7 +614,7 @@
width: imageProxy.width);
weakThis.target!.cameraImageDataStreamController!.add(cameraImageData);
- imageProxy.close();
+ unawaited(imageProxy.close());
}
// shouldCreateDetachedObjectForTesting is used to create an Analyzer
@@ -627,7 +627,7 @@
// TODO(camsim99): Support resolution configuration.
// Defaults to YUV_420_888 image format.
imageAnalysis = createImageAnalysis(null);
- imageAnalysis!.setAnalyzer(analyzer);
+ unawaited(imageAnalysis!.setAnalyzer(analyzer));
// TODO(camsim99): Reset live camera state observers here when
// https://github.com/flutter/packages/pull/3419 lands.
@@ -652,7 +652,7 @@
/// The [onListen] callback for the stream controller used for image
/// streaming.
Future<void> _onFrameStreamListen() async {
- _configureAndBindImageAnalysisToLifecycle();
+ await _configureAndBindImageAnalysisToLifecycle();
}
/// The [onCancel] callback for the stream controller used for image
@@ -661,7 +661,7 @@
/// Removes the previously set analyzer on the [imageAnalysis] instance, since
/// image information should no longer be streamed.
FutureOr<void> _onFrameStreamCancel() async {
- imageAnalysis!.clearAnalyzer();
+ unawaited(imageAnalysis!.clearAnalyzer());
}
/// Converts between Android ImageFormat constants and [ImageFormatGroup]s.
@@ -687,7 +687,7 @@
/// removed, as well.
Future<void> _updateLiveCameraState(int cameraId) async {
final CameraInfo cameraInfo = await camera!.getCameraInfo();
- liveCameraState?.removeObservers();
+ await liveCameraState?.removeObservers();
liveCameraState = await cameraInfo.getCameraState();
await liveCameraState!.observe(_createCameraClosingObserver(cameraId));
}
diff --git a/packages/camera/camera_android_camerax/lib/src/image_capture.dart b/packages/camera/camera_android_camerax/lib/src/image_capture.dart
index 6a9b2de..4df8f29 100644
--- a/packages/camera/camera_android_camerax/lib/src/image_capture.dart
+++ b/packages/camera/camera_android_camerax/lib/src/image_capture.dart
@@ -139,7 +139,7 @@
assert(identifier != null,
'No ImageCapture has the identifer of that requested to get the resolution information for.');
- setFlashMode(identifier!, flashMode);
+ await setFlashMode(identifier!, flashMode);
}
/// Takes a picture with the specified [ImageCapture] instance.
diff --git a/packages/camera/camera_android_camerax/lib/src/recording.dart b/packages/camera/camera_android_camerax/lib/src/recording.dart
index 2f21e25..5d7c54c 100644
--- a/packages/camera/camera_android_camerax/lib/src/recording.dart
+++ b/packages/camera/camera_android_camerax/lib/src/recording.dart
@@ -67,22 +67,22 @@
/// Closes the specified recording instance.
Future<void> closeFromInstance(Recording recording) async {
- close(instanceManager.getIdentifier(recording)!);
+ await close(instanceManager.getIdentifier(recording)!);
}
/// Pauses the specified recording instance if active.
Future<void> pauseFromInstance(Recording recording) async {
- pause(instanceManager.getIdentifier(recording)!);
+ await pause(instanceManager.getIdentifier(recording)!);
}
/// Resumes the specified recording instance if paused.
Future<void> resumeFromInstance(Recording recording) async {
- resume(instanceManager.getIdentifier(recording)!);
+ await resume(instanceManager.getIdentifier(recording)!);
}
/// Stops the specified recording instance, as if calling closeFromInstance().
Future<void> stopFromInstance(Recording recording) async {
- stop(instanceManager.getIdentifier(recording)!);
+ await stop(instanceManager.getIdentifier(recording)!);
}
}
diff --git a/packages/camera/camera_android_camerax/pubspec.yaml b/packages/camera/camera_android_camerax/pubspec.yaml
index d036418..bc629f7 100644
--- a/packages/camera/camera_android_camerax/pubspec.yaml
+++ b/packages/camera/camera_android_camerax/pubspec.yaml
@@ -2,7 +2,7 @@
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+7
+version: 0.5.0+8
environment:
sdk: ">=2.19.0 <4.0.0"
diff --git a/packages/camera/camera_android_camerax/test/android_camera_camerax_test.dart b/packages/camera/camera_android_camerax/test/android_camera_camerax_test.dart
index 63b7f64..258a834 100644
--- a/packages/camera/camera_android_camerax/test/android_camera_camerax_test.dart
+++ b/packages/camera/camera_android_camerax/test/android_camera_camerax_test.dart
@@ -347,7 +347,7 @@
camera.liveCameraState = MockLiveCameraState();
camera.imageAnalysis = MockImageAnalysis();
- camera.dispose(3);
+ await camera.dispose(3);
verify(camera.preview!.releaseFlutterSurfaceTexture());
verify(camera.liveCameraState!.removeObservers());
@@ -689,7 +689,7 @@
final AndroidCameraCameraX camera = AndroidCameraCameraX();
final MockRecording recording = MockRecording();
camera.recording = recording;
- camera.pauseVideoRecording(0);
+ await camera.pauseVideoRecording(0);
verify(recording.pause());
verifyNoMoreInteractions(recording);
});
@@ -698,7 +698,7 @@
final AndroidCameraCameraX camera = AndroidCameraCameraX();
final MockRecording recording = MockRecording();
camera.recording = recording;
- camera.resumeVideoRecording(0);
+ await camera.resumeVideoRecording(0);
verify(recording.resume());
verifyNoMoreInteractions(recording);
});
@@ -974,7 +974,7 @@
// Verify camera and cameraInfo were properly updated.
expect(camera.camera, equals(mockCamera));
expect(camera.cameraInfo, equals(mockCameraInfo));
- onStreamedFrameAvailableSubscription.cancel();
+ await onStreamedFrameAvailableSubscription.cancel();
});
test(
diff --git a/packages/camera/camera_android_camerax/test/recording_test.dart b/packages/camera/camera_android_camerax/test/recording_test.dart
index 06de01f..b0877e9 100644
--- a/packages/camera/camera_android_camerax/test/recording_test.dart
+++ b/packages/camera/camera_android_camerax/test/recording_test.dart
@@ -37,7 +37,7 @@
instanceManager.addHostCreatedInstance(recording, recordingId,
onCopy: (_) => Recording.detached(instanceManager: instanceManager));
- recording.close();
+ await recording.close();
verify(mockApi.close(recordingId));
});
@@ -57,7 +57,7 @@
instanceManager.addHostCreatedInstance(recording, recordingId,
onCopy: (_) => Recording.detached(instanceManager: instanceManager));
- recording.pause();
+ await recording.pause();
verify(mockApi.pause(recordingId));
});
@@ -77,7 +77,7 @@
instanceManager.addHostCreatedInstance(recording, recordingId,
onCopy: (_) => Recording.detached(instanceManager: instanceManager));
- recording.resume();
+ await recording.resume();
verify(mockApi.resume(recordingId));
});
@@ -97,7 +97,7 @@
instanceManager.addHostCreatedInstance(recording, recordingId,
onCopy: (_) => Recording.detached(instanceManager: instanceManager));
- recording.stop();
+ await recording.stop();
verify(mockApi.stop(recordingId));
});
diff --git a/script/configs/custom_analysis.yaml b/script/configs/custom_analysis.yaml
index 1e6d9bd..79d76d1 100644
--- a/script/configs/custom_analysis.yaml
+++ b/script/configs/custom_analysis.yaml
@@ -13,7 +13,6 @@
# Temporary opt-outs of unawaited_futures; see
# https://github.com/flutter/flutter/issues/127323
-- camera/camera_android_camerax
- webview_flutter/webview_flutter
- webview_flutter/webview_flutter_android
- webview_flutter/webview_flutter_wkwebview