Skip to content

Commit 1809a6c

Browse files
authored
[camerax] Fixes unawaited_futures violations (flutter#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 flutter#127323.
1 parent 415bf6d commit 1809a6c

File tree

10 files changed

+32
-37
lines changed

10 files changed

+32
-37
lines changed

packages/camera/camera_android_camerax/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.0+8
2+
3+
* Fixes unawaited_futures violations.
4+
15
## 0.5.0+7
26

37
* Updates Guava version to 32.0.1.

packages/camera/camera_android_camerax/analysis_options.yaml

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/camera/camera_android_camerax/example/lib/camera_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ class CameraController extends ValueNotifier<CameraValue> {
526526
}
527527

528528
if (value.isStreamingImages) {
529-
stopImageStream();
529+
unawaited(stopImageStream());
530530
}
531531

532532
try {

packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,9 @@ class AndroidCameraCameraX extends CameraPlatform {
317317
@override
318318
Future<void> dispose(int cameraId) async {
319319
preview?.releaseFlutterSurfaceTexture();
320-
liveCameraState?.removeObservers();
320+
unawaited(liveCameraState?.removeObservers());
321321
processCameraProvider?.unbindAll();
322-
imageAnalysis?.clearAnalyzer();
322+
unawaited(imageAnalysis?.clearAnalyzer());
323323
}
324324

325325
/// The camera has been initialized.
@@ -423,17 +423,17 @@ class AndroidCameraCameraX extends CameraPlatform {
423423
/// [cameraId] not used.
424424
@override
425425
Future<void> pausePreview(int cameraId) async {
426-
_unbindUseCaseFromLifecycle(preview!);
427426
_previewIsPaused = true;
427+
await _unbindUseCaseFromLifecycle(preview!);
428428
}
429429

430430
/// Resume the paused preview for the selected camera.
431431
///
432432
/// [cameraId] not used.
433433
@override
434434
Future<void> resumePreview(int cameraId) async {
435-
await _bindPreviewToLifecycle(cameraId);
436435
_previewIsPaused = false;
436+
await _bindPreviewToLifecycle(cameraId);
437437
}
438438

439439
/// Returns a widget showing a live camera preview.
@@ -507,7 +507,7 @@ class AndroidCameraCameraX extends CameraPlatform {
507507
if (videoOutputPath == null) {
508508
// Stop the current active recording as we will be unable to complete it
509509
// in this error case.
510-
recording!.close();
510+
unawaited(recording!.close());
511511
recording = null;
512512
pendingRecording = null;
513513
throw CameraException(
@@ -516,7 +516,7 @@ class AndroidCameraCameraX extends CameraPlatform {
516516
'while reporting success. The platform should always '
517517
'return a valid path or report an error.');
518518
}
519-
recording!.close();
519+
unawaited(recording!.close());
520520
recording = null;
521521
pendingRecording = null;
522522
return XFile(videoOutputPath!);
@@ -526,15 +526,15 @@ class AndroidCameraCameraX extends CameraPlatform {
526526
@override
527527
Future<void> pauseVideoRecording(int cameraId) async {
528528
if (recording != null) {
529-
recording!.pause();
529+
await recording!.pause();
530530
}
531531
}
532532

533533
/// Resume the current video recording if it is not null.
534534
@override
535535
Future<void> resumeVideoRecording(int cameraId) async {
536536
if (recording != null) {
537-
recording!.resume();
537+
await recording!.resume();
538538
}
539539
}
540540

@@ -614,7 +614,7 @@ class AndroidCameraCameraX extends CameraPlatform {
614614
width: imageProxy.width);
615615

616616
weakThis.target!.cameraImageDataStreamController!.add(cameraImageData);
617-
imageProxy.close();
617+
unawaited(imageProxy.close());
618618
}
619619

620620
// shouldCreateDetachedObjectForTesting is used to create an Analyzer
@@ -627,7 +627,7 @@ class AndroidCameraCameraX extends CameraPlatform {
627627
// TODO(camsim99): Support resolution configuration.
628628
// Defaults to YUV_420_888 image format.
629629
imageAnalysis = createImageAnalysis(null);
630-
imageAnalysis!.setAnalyzer(analyzer);
630+
unawaited(imageAnalysis!.setAnalyzer(analyzer));
631631

632632
// TODO(camsim99): Reset live camera state observers here when
633633
// https://github.com/flutter/packages/pull/3419 lands.
@@ -652,7 +652,7 @@ class AndroidCameraCameraX extends CameraPlatform {
652652
/// The [onListen] callback for the stream controller used for image
653653
/// streaming.
654654
Future<void> _onFrameStreamListen() async {
655-
_configureAndBindImageAnalysisToLifecycle();
655+
await _configureAndBindImageAnalysisToLifecycle();
656656
}
657657

658658
/// The [onCancel] callback for the stream controller used for image
@@ -661,7 +661,7 @@ class AndroidCameraCameraX extends CameraPlatform {
661661
/// Removes the previously set analyzer on the [imageAnalysis] instance, since
662662
/// image information should no longer be streamed.
663663
FutureOr<void> _onFrameStreamCancel() async {
664-
imageAnalysis!.clearAnalyzer();
664+
unawaited(imageAnalysis!.clearAnalyzer());
665665
}
666666

667667
/// Converts between Android ImageFormat constants and [ImageFormatGroup]s.
@@ -687,7 +687,7 @@ class AndroidCameraCameraX extends CameraPlatform {
687687
/// removed, as well.
688688
Future<void> _updateLiveCameraState(int cameraId) async {
689689
final CameraInfo cameraInfo = await camera!.getCameraInfo();
690-
liveCameraState?.removeObservers();
690+
await liveCameraState?.removeObservers();
691691
liveCameraState = await cameraInfo.getCameraState();
692692
await liveCameraState!.observe(_createCameraClosingObserver(cameraId));
693693
}

packages/camera/camera_android_camerax/lib/src/image_capture.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class ImageCaptureHostApiImpl extends ImageCaptureHostApi {
139139
assert(identifier != null,
140140
'No ImageCapture has the identifer of that requested to get the resolution information for.');
141141

142-
setFlashMode(identifier!, flashMode);
142+
await setFlashMode(identifier!, flashMode);
143143
}
144144

145145
/// Takes a picture with the specified [ImageCapture] instance.

packages/camera/camera_android_camerax/lib/src/recording.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,22 @@ class RecordingHostApiImpl extends RecordingHostApi {
6767

6868
/// Closes the specified recording instance.
6969
Future<void> closeFromInstance(Recording recording) async {
70-
close(instanceManager.getIdentifier(recording)!);
70+
await close(instanceManager.getIdentifier(recording)!);
7171
}
7272

7373
/// Pauses the specified recording instance if active.
7474
Future<void> pauseFromInstance(Recording recording) async {
75-
pause(instanceManager.getIdentifier(recording)!);
75+
await pause(instanceManager.getIdentifier(recording)!);
7676
}
7777

7878
/// Resumes the specified recording instance if paused.
7979
Future<void> resumeFromInstance(Recording recording) async {
80-
resume(instanceManager.getIdentifier(recording)!);
80+
await resume(instanceManager.getIdentifier(recording)!);
8181
}
8282

8383
/// Stops the specified recording instance, as if calling closeFromInstance().
8484
Future<void> stopFromInstance(Recording recording) async {
85-
stop(instanceManager.getIdentifier(recording)!);
85+
await stop(instanceManager.getIdentifier(recording)!);
8686
}
8787
}
8888

packages/camera/camera_android_camerax/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: camera_android_camerax
22
description: Android implementation of the camera plugin using the CameraX library.
33
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
5-
version: 0.5.0+7
5+
version: 0.5.0+8
66

77
environment:
88
sdk: ">=2.19.0 <4.0.0"

packages/camera/camera_android_camerax/test/android_camera_camerax_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ void main() {
347347
camera.liveCameraState = MockLiveCameraState();
348348
camera.imageAnalysis = MockImageAnalysis();
349349

350-
camera.dispose(3);
350+
await camera.dispose(3);
351351

352352
verify(camera.preview!.releaseFlutterSurfaceTexture());
353353
verify(camera.liveCameraState!.removeObservers());
@@ -689,7 +689,7 @@ void main() {
689689
final AndroidCameraCameraX camera = AndroidCameraCameraX();
690690
final MockRecording recording = MockRecording();
691691
camera.recording = recording;
692-
camera.pauseVideoRecording(0);
692+
await camera.pauseVideoRecording(0);
693693
verify(recording.pause());
694694
verifyNoMoreInteractions(recording);
695695
});
@@ -698,7 +698,7 @@ void main() {
698698
final AndroidCameraCameraX camera = AndroidCameraCameraX();
699699
final MockRecording recording = MockRecording();
700700
camera.recording = recording;
701-
camera.resumeVideoRecording(0);
701+
await camera.resumeVideoRecording(0);
702702
verify(recording.resume());
703703
verifyNoMoreInteractions(recording);
704704
});
@@ -974,7 +974,7 @@ void main() {
974974
// Verify camera and cameraInfo were properly updated.
975975
expect(camera.camera, equals(mockCamera));
976976
expect(camera.cameraInfo, equals(mockCameraInfo));
977-
onStreamedFrameAvailableSubscription.cancel();
977+
await onStreamedFrameAvailableSubscription.cancel();
978978
});
979979

980980
test(

packages/camera/camera_android_camerax/test/recording_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void main() {
3737
instanceManager.addHostCreatedInstance(recording, recordingId,
3838
onCopy: (_) => Recording.detached(instanceManager: instanceManager));
3939

40-
recording.close();
40+
await recording.close();
4141

4242
verify(mockApi.close(recordingId));
4343
});
@@ -57,7 +57,7 @@ void main() {
5757
instanceManager.addHostCreatedInstance(recording, recordingId,
5858
onCopy: (_) => Recording.detached(instanceManager: instanceManager));
5959

60-
recording.pause();
60+
await recording.pause();
6161

6262
verify(mockApi.pause(recordingId));
6363
});
@@ -77,7 +77,7 @@ void main() {
7777
instanceManager.addHostCreatedInstance(recording, recordingId,
7878
onCopy: (_) => Recording.detached(instanceManager: instanceManager));
7979

80-
recording.resume();
80+
await recording.resume();
8181

8282
verify(mockApi.resume(recordingId));
8383
});
@@ -97,7 +97,7 @@ void main() {
9797
instanceManager.addHostCreatedInstance(recording, recordingId,
9898
onCopy: (_) => Recording.detached(instanceManager: instanceManager));
9999

100-
recording.stop();
100+
await recording.stop();
101101

102102
verify(mockApi.stop(recordingId));
103103
});

script/configs/custom_analysis.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
# Temporary opt-outs of unawaited_futures; see
1515
# https://github.com/flutter/flutter/issues/127323
16-
- camera/camera_android_camerax
1716
- webview_flutter/webview_flutter
1817
- webview_flutter/webview_flutter_android
1918
- webview_flutter/webview_flutter_wkwebview

0 commit comments

Comments
 (0)