Skip to main content

avoid-async-call-in-sync-function

added in: 1.8.0

Warns when an async function is invoked in non-async blocks.

Making asynchronous calls in non-async functions is usually the sign of a programming error. In general these functions should be marked async and such futures should likely be awaited.

This rule is an improved version of https://dart.dev/tools/linter-rules/discarded_futures.

Example

❌ Bad:

Future<void> asyncValue() async => 'value';

class SomeClass {
SomeClass() {
asyncValue(); // LINT
}

Future<void> asyncMethod() => asyncValue();

Future<void> anotherAsyncMethod() async => asyncValue();

void syncFn() => asyncValue(); // LINT
}

✅ Good:

Future<void> asyncValue() async => 'value';

class SomeClass {
SomeClass() {
unawaited(asyncValue());
}

Future<void> asyncMethod() => asyncValue();

Future<void> anotherAsyncMethod() async => asyncValue();
}