prefer_async_await

SeverityQuick FixOptions
Info

Details

DO use async/await instead of using raw futures.

Using async/await improves code readability.

see: effective-dart

Bad
void fetchData() {
  performAsyncOperation().then((result) {
    print(result);
  }).catchError((Object? error) {
    print('Error: $error');
  }).whenComplete(() {
    print('Done');
  });
}
Good
Future<void> fetchData() async {
  try {
    final result = await performAsyncOperation();
    print(result);
  } catch (error) {
    print('Error: $error');
  } finally {
    print('Done');
  }
}

Usage

To enable the prefer_async_await rule, add prefer_async_await under custom_lint > rules in your analysis_options.yaml file:

custom_lint:
  rules:
    - prefer_async_await