avoid-redundant-async

Same as avoid-redundant-async in Dart Code Metrics

Path
lint/complexity/avoid-redundant-async
Default
On (recommended)
Origin
Dart Code Metrics
Rule
121 of 512
recommended

Flags an async function whose body is a single return await ...;.

Returning the Future directly can simplify these wrappers, but synchronous throws then escape instead of completing the returned future with an error. Only top-level functions and methods with a single return await statement are checked, not local functions or anonymous closures.

Invalid

example.dartdart
// Bad: async with only one await and no error handling
Future<String> getName() async {
  return await db.getName();
}

Future<int> getValue() async {
  return await compute();
}

class Service {
  Future<User> getUser(String id) async {
    return await repository.fetchUser(id);
  }

  Future<List<String>> getNames() async {
    return await api.names();
  }
}

Future<dynamic> fetch(String url) async {
  return await http.get(url);
}

Future<String> transform(String input) async {
  return await processString(input);
}
Remove `async` and `await` to return the `Future` directly.
1// Bad: async with only one await and no error handling
2Future<String> getName() async {
Remove `async` and `await` to return the `Future` directly.
5
6Future<int> getValue() async {
Remove `async` and `await` to return the `Future` directly.
10class Service {
11 Future<User> getUser(String id) async {
Remove `async` and `await` to return the `Future` directly.
14
15 Future<List<String>> getNames() async {
Remove `async` and `await` to return the `Future` directly.
19
20Future<dynamic> fetch(String url) async {
Remove `async` and `await` to return the `Future` directly.
23
24Future<String> transform(String input) async {

Valid

example.dartdart
// Good: removing unnecessary async/await
Future<String> getName() => db.getName();

Future<int> getValue() => compute();

class Service {
  Future<User> getUser(String id) => repository.fetchUser(id);

  Future<List<String>> getNames() => api.names();
}

Future<dynamic> fetch(String url) => http.get(url);

Future<String> transform(String input) => processString(input);

// OK: async with error handling
Future<String> safeGetName() async {
  try {
    return await db.getName();
  } catch (e) {
    return 'Default';
  }
}

// OK: async with multiple awaits
Future<User> buildUser(String id) async {
  final userData = await repository.fetchUser(id);
  final preferences = await repository.fetchPreferences(id);
  return User(userData, preferences);
}

// OK: async with additional logic
Future<String> processName() async {
  final name = await db.getName();
  print('Loaded: $name');
  return name.toUpperCase();
}

Configure avoid-redundant-async

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "avoid-redundant-async": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/complexity/avoid-redundant-async: <reason>