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.
If not awaiting a future is the correct behavior, consider adding unawaited(...)
or .ignore()
to clearly indicate the intent. Note that .ignore()
will also suppress any exceptions.
Example
❌ Bad:
Future<void> asyncValue() async => 'value';
class SomeClass {
SomeClass() {
// LINT: Avoid invoking async functions in non-async blocks.
// Try awaiting it, wrapping in 'unawaited(...)' or calling '.ignore()'.
asyncValue();
}
// LINT: Avoid invoking async functions in non-async blocks.
// Try awaiting it, wrapping in 'unawaited(...)' or calling '.ignore()'.
void syncFn() => asyncValue();
}
✅ Good:
Future<void> asyncValue() async => 'value';
class SomeClass {
SomeClass() {
unawaited(asyncValue()); // Correct, unawaited
}
Future<void> asyncMethod() async => asyncValue(); // Correct, async method
}
Additional Resources
- This rule is an improved version of https://dart.dev/tools/linter-rules/discarded_futures.