Skip to main content

Prefer async await

Rule id

prefer-async-await

Severity

Style

Description

Recommends to use async/await syntax to handle Futures result instead of .then() invocation. Also can help prevent errors with mixed await and .then() usages, since awaiting the result of a Future with .then() invocation awaits the completion of .then().

Example

Bad:

Future<void> main() async {
someFuture.then((result) => handleResult(result)); // LINT

await (foo.asyncMethod()).then((result) => handleResult(result)); // LINT
}

Good:

Future<void> main() async {
final result = await someFuture;
handleResult(result);

final anotherResult = await foo.asyncMethod();
handleResult(anotherResult);
}