Skip to content

Commit 97e3ba6

Browse files
[various] Fixes unawaited_futures violations (#4067)
This option had been disabled to match flutter/flutter, but the reason it was disabled there was "too many false positives", mostly around animation. That doesn't apply to most packages here, and we've had a number of production bugs�especially in plugins, that use async heavily in ways that are intended to be client-awaitable�that this would have caught. This PR: - Enables the option at the repo level. - Permanently (unless the owners decide to change it) opts out `animations` and `go_router`, both of which looked like mostly or entirely false positives. - Temporarily opted out a few plugins that have a lot of violations that should be handled in their own PRs later (`camera_android_camerax`, most of `webview_flutter`). - Fixes all remaining violations. In many cases this PR is behavior-changing, replacing implicitly unawaited futures that did not seem obviously intentional with `await`ed futures, so non-test code in particular should be reviewed carefully to make sure the changes are correct. All of the changes are manual, not `fix`-generated. Part of flutter/flutter#127323
1 parent f19282a commit 97e3ba6

File tree

78 files changed

+266
-143
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+266
-143
lines changed

analysis_options.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ linter:
183183
- tighten_type_of_initializing_formals
184184
# - type_annotate_public_apis # subset of always_specify_types
185185
- type_init_formals
186-
# - unawaited_futures # too many false positives, especially with the way AnimationController works
186+
- unawaited_futures # DIFFERENT FROM FLUTTER/FLUTTER: It's disabled there for "too many false positives"; that's not an issue here, and missing awaits have caused production issues in plugins.
187187
- unnecessary_await_in_return
188188
- unnecessary_brace_in_string_interps
189189
- unnecessary_const
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This custom rule set only exists to allow opting out of the repository
2+
# default of enabling unawaited_futures. Please do NOT add more changes
3+
# here without consulting with #hackers-ecosystem on Discord.
4+
5+
include: ../../analysis_options.yaml
6+
7+
linter:
8+
rules:
9+
# Matches flutter/flutter, which disables this rule due to false positives.
10+
unawaited_futures: false

packages/camera/camera/CHANGELOG.md

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

37
* Removes obsolete null checks on non-nullable values.

packages/camera/camera/lib/src/camera_controller.dart

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

554554
if (value.isStreamingImages) {
555-
stopImageStream();
555+
await stopImageStream();
556556
}
557557

558558
try {

packages/camera/camera/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: A Flutter plugin for controlling the camera. Supports previewing
44
Dart.
55
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
7-
version: 0.10.5+1
7+
version: 0.10.5+2
88

99
environment:
1010
sdk: ">=2.18.0 <4.0.0"

packages/camera/camera/test/camera_image_stream_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void main() {
174174

175175
await cameraController.initialize();
176176

177-
cameraController.startVideoRecording(
177+
await cameraController.startVideoRecording(
178178
onAvailable: (CameraImage image) => null);
179179

180180
expect(
@@ -192,7 +192,7 @@ void main() {
192192

193193
await cameraController.initialize();
194194

195-
cameraController.startVideoRecording();
195+
await cameraController.startVideoRecording();
196196

197197
expect(mockPlatform.streamCallLog.contains('startVideoCapturing'), isTrue);
198198
});

packages/camera/camera_android/CHANGELOG.md

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

37
* Removes obsolete null checks on non-nullable values.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ class CameraController extends ValueNotifier<CameraValue> {
229229
enableAudio: enableAudio,
230230
);
231231

232-
CameraPlatform.instance
232+
unawaited(CameraPlatform.instance
233233
.onCameraInitialized(_cameraId)
234234
.first
235235
.then((CameraInitializedEvent event) {
236236
initializeCompleter.complete(event);
237-
});
237+
}));
238238

239239
await CameraPlatform.instance.initializeCamera(
240240
_cameraId,
@@ -444,8 +444,8 @@ class CameraController extends ValueNotifier<CameraValue> {
444444
if (_isDisposed) {
445445
return;
446446
}
447-
_deviceOrientationSubscription?.cancel();
448447
_isDisposed = true;
448+
await _deviceOrientationSubscription?.cancel();
449449
super.dispose();
450450
if (_initCalled != null) {
451451
await _initCalled;

packages/camera/camera_android/test/android_camera_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ void main() {
11191119
isMethodCall('startImageStream', arguments: null),
11201120
]);
11211121

1122-
subscription.cancel();
1122+
await subscription.cancel();
11231123
});
11241124

11251125
test('Should stop streaming', () async {
@@ -1136,7 +1136,7 @@ void main() {
11361136
final StreamSubscription<CameraImageData> subscription = camera
11371137
.onStreamedFrameAvailable(cameraId)
11381138
.listen((CameraImageData imageData) {});
1139-
subscription.cancel();
1139+
await subscription.cancel();
11401140

11411141
// Assert
11421142
expect(channel.log, <Matcher>[
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# TODO(stuartmorgan): Remove this file and fix all the unawaited_futures
2+
# violations. See https://github.com/flutter/flutter/issues/127323
3+
4+
include: ../../../analysis_options.yaml
5+
6+
linter:
7+
rules:
8+
unawaited_futures: false

0 commit comments

Comments
 (0)