avoid-async-call-in-sync-function
Warns when an async function is invoked in non-async blocks.
Making asynchronous calls in non-asynchronous functions is usually a sign of a bug. In general, such functions should be marked async
, and such futures should likely be awaited.
Example
❌ Bad:
Future<void> asyncValue() async => 'value';
class SomeClass {
SomeClass() {
asyncValue(); // LINT: Avoid invoking async functions in non-async blocks. Try awaiting it, wrapping in 'unawaited(...)' or calling '.ignore()'.
}
Future<void> asyncMethod() => asyncValue();
Future<void> anotherAsyncMethod() async => asyncValue();
void syncFn() => asyncValue(); // LINT: Avoid invoking async functions in non-async blocks. Try awaiting it, wrapping in 'unawaited(...)' or calling '.ignore()'.
}
✅ Good:
Future<void> asyncValue() async => 'value';
class SomeClass {
SomeClass() {
unawaited(asyncValue()); // Correct, unawaited
}
Future<void> asyncMethod() => asyncValue();
Future<void> anotherAsyncMethod() async => asyncValue(); // Correct, async method
}
Additional Resources
- This rule is an improved version of https://dart.dev/tools/linter-rules/discarded_futures.