Add @awaitNotRequired annotation to flutter sdk#181513
Add @awaitNotRequired annotation to flutter sdk#181513victorsanni wants to merge 12 commits intoflutter:masterfrom
Conversation
|
Do we have a plan on the criteria that justify a |
There is a lint ( |
| } | ||
|
|
||
| @awaitNotRequired | ||
| Future<void> _rebuild([void value]) { |
There was a problem hiding this comment.
It seems like the returned future of this private method is never used. Why can't it simply return void?
There was a problem hiding this comment.
True, i think it was initially added as a Future<void> to satisfy the .then API, but no longer needed to after #100657.
| /// Returns a [Future] which completes to the received response, which may | ||
| /// be null. | ||
| @awaitNotRequired | ||
| Future<T?> send(T message) async { |
There was a problem hiding this comment.
Should we really mark send and invokeMethod as awaitNotRequired? IMO these foundational methods should require await and let individual usages suppress the warning explicitly.
There was a problem hiding this comment.
I agree, there are other callsites in the framework with await. Should we suppress the warning with an ignore or add awaits?
There was a problem hiding this comment.
I suspect many such usages are intentional, probably because those send do not wait for responses. For such cases it's ok to explicitly ignore while commenting that it's intentional to not wait for response. LMK if there are other cases that you're unsure of.
| /// The method by which [golden] is located and by which its bytes are written | ||
| /// is left up to the implementation class. | ||
| @awaitNotRequired | ||
| Future<void> update(Uri golden, Uint8List imageBytes); |
There was a problem hiding this comment.
I feel like this should enforce await. Can you check where it is used?
There was a problem hiding this comment.
Agree, most of its callsites use await, its non-await callsites are primarily from this test file in this PR: #160484.
I can go ahead and add await to these callsites.
|
Thanks for the review @dkwingsmt. I didn't think there are situations in the framework where some Is this review exhaustive? i.e are the methods you've pointed out the only ones that should have |
|
I didn't review the ones under |
There was a problem hiding this comment.
Code Review
This pull request introduces the @awaitNotRequired annotation across the Flutter SDK to address unawaited_futures lint warnings. The changes are extensive, touching many files to annotate methods that return a Future but are not intended to be awaited, such as animation controllers, navigation methods, and platform channel invocations. This makes the code cleaner and the intent clearer. Additionally, the PR fixes a few instances in tests where tester.pump() was not correctly awaited. The changes are well-executed and improve the overall code quality by adhering to linting best practices.
|
Related: #182233 void A() {
assert(false); // Exception thrown
}
Future<void> animateTo() async {
A();
}
void handleEvent() { // Sync handler
animateTo(); // The future is not awaited.
}
void D() {
try {
handleEvent(); // D thought this was enough catching.
} catch {
FlutterError.report(...);
}
}This shows the risk of allowing futures to not be awaited: Async errors will not be caught at all. Therefore I think we should not allow futures to be un-awaited (except maybe in tests, with discretion). All futures should either be awaited or have a |
| } | ||
|
|
||
| @awaitNotRequired | ||
| Future<void> start() async { |
There was a problem hiding this comment.
Hi @dkwingsmt, I see you added this method (and its other clones below) in #59803, did you intend to use it in the postFrameCallback without await?
There was a problem hiding this comment.
I think awaitNotRequired is ok here, since this is only used in a fully managed test that I expect no errors to occur, and I'm ok with errors being thrown.
| _canceled = true; | ||
| } | ||
|
|
||
| @awaitNotRequired |
There was a problem hiding this comment.
I think we should keep the annotation here. This PR added it: #79163? WDYT @dkwingsmt
There was a problem hiding this comment.
Yeah, I think having no await is the intention here.
Impl:
```dart
/// Sends the specified [message] to the platform plugins on this channel.
///
/// Returns a [Future] which completes to the received response, which may
/// be null.
Future<T?> send(T message) async {
return codec.decodeMessage(await binaryMessenger.send(name, codec.encodeMessage(message)));
}
```
Part of #181513
Part of [Add @awaitNotRequired annotation to flutter sdk](#181513)
Impl:
```dart
/// Sends the specified [message] to the platform plugins on this channel.
///
/// Returns a [Future] which completes to the received response, which may
/// be null.
Future<T?> send(T message) async {
return codec.decodeMessage(await binaryMessenger.send(name, codec.encodeMessage(message)));
}
```
Part of flutter#181513
…er#183334) Part of [Add @awaitNotRequired annotation to flutter sdk](flutter#181513)
Part of [Add @awaitNotRequired annotation to flutter sdk](#181513)
| /// which switches to [AnimationStatus.completed] when [upperBound] is | ||
| /// reached at the end of the animation. | ||
| @awaitNotRequired | ||
| TickerFuture forward({double? from}) { |
There was a problem hiding this comment.
Add comment to TickerFuture impl:
A TickerFuture should probably NOT be awaited at all, since it is risky to hang forever. Instead, people who wish to know when/whether it completes will explicitly attach a callback anyway. This should make it reasonable to make mark all TickerFuture as awaitNotRequired.
Part of [Add @awaitNotRequired annotation to flutter sdk](#181513)
Part of [Add @awaitNotRequired annotation to flutter sdk](#181513)
Draft to fix Use @awaitNotRequired in Flutter SDK
Turned on the
unawaited_futureslint and added the annotation to every function declared in the flutter SDK and flagged by the linter. This means the annotation is only added toFuture-returning functions called at least once without theawaitkeyword preceding it. So if there exists aFuture-returning function in the Flutter SDK that does not need to beawaited, but all call sites in the SDK have theawaitkeyword, it is not annotated.EDIT: Opened PRs to add
awaits orignores to callsites instead of adding the annotation. Some functions may still require this annotation, but this set is much smaller than originally anticipated.Missing annotation:
integrationDriver:flutter/packages/integration_test/lib/integration_test_driver_extended.dart
Line 77 in 2c74ab1
Because this will require incurring a dependency on
package:meta/meta.dartinpackage:integration_test/integration_test_driver.dart.