discarded_ futures
Learn about the discarded_futures linter rule.
There should be no Future-returning calls in
synchronous functions unless they are assigned or returned.
Details
#
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 (as enforced by
unawaited_futures).
DON'T invoke asynchronous functions in
non-async blocks.
BAD:
void recreateDir(String path) {
deleteDir(path);
createDir(path);
}
Future<void> deleteDir(String path) async {}
Future<void> createDir(String path) async {}
GOOD:
Future<void> recreateDir(String path) async {
await deleteDir(path);
await createDir(path);
}
Future<void> deleteDir(String path) async {}
Future<void> createDir(String path) async {}
Enable
#
To enable the discarded_futures rule, add
discarded_futures under
linter > rules in your
analysis_options.yaml
file:
linter:
rules:
- discarded_futures
If you're instead using the YAML map syntax to configure
linter rules, add discarded_futures: true under
linter > rules:
linter:
rules:
discarded_futures: true
除非另有说明,文档之所提及适用于 Dart 3.12.2 版本报告页面问题.