avoid-slow-async-io
Same as avoid_slow_async_io in Dart lints
- Group
- Performance
- Path
lint/performance/avoid-slow-async-io- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 142 of 512
Avoid the slow asynchronous dart:io file-system queries.
These methods are much slower than their synchronous counterparts because each one round-trips through the event loop for a tiny amount of work:
if (await file.exists()) {} // BAD
if (file.existsSync()) {} // GOOD
Reported calls: File.lastModified, the FileSystemEntity instance methods
exists and stat, and the static FileSystemEntity.isDirectory,
isFile, isLink, and type (also through an import prefix). Instance
calls count when the invoked member is the dart:io declaration, also
through a project class or extension type implementing File,
Directory, Link, or FileSystemEntity that does not declare it.
Receivers whose type is unresolved stay silent.
Invalid
import 'dart:io';
Future<void> fileQueries(File file) async {
await file.lastModified();
await file.exists();
await file.stat();
}
Future<void> directoryQueries(Directory dir) async {
await dir.exists();
await dir.stat();
}
Future<void> linkQueries(Link link) async {
await link.exists();
}
Future<void> staticQueries(String path) async {
await FileSystemEntity.isDirectory(path);
await FileSystemEntity.isFile(path);
await FileSystemEntity.isLink(path);
await FileSystemEntity.type(path);
}
Future<void> localFile() async {
final file = File('data.txt');
if (await file.exists()) {
print('present');
}
}Future<void> fileQueries(File file) async { await file.lastModified(); await file.lastModified(); await file.exists(); await file.exists(); await file.stat();Future<void> directoryQueries(Directory dir) async { await dir.exists(); await dir.exists(); await dir.stat();Future<void> linkQueries(Link link) async { await link.exists();Future<void> staticQueries(String path) async { await FileSystemEntity.isDirectory(path); await FileSystemEntity.isDirectory(path); await FileSystemEntity.isFile(path); await FileSystemEntity.isFile(path); await FileSystemEntity.isLink(path); await FileSystemEntity.isLink(path); await FileSystemEntity.type(path); final file = File('data.txt'); if (await file.exists()) {Valid
import 'dart:io';
class Store {
Future<bool> exists() async => true;
Future<DateTime> lastModified() async => DateTime.now();
}
void syncQueries(File file, Directory dir, String path) {
file.lastModifiedSync();
file.existsSync();
file.statSync();
dir.existsSync();
dir.statSync();
FileSystemEntity.isDirectorySync(path);
FileSystemEntity.isFileSync(path);
FileSystemEntity.isLinkSync(path);
FileSystemEntity.typeSync(path);
}
Future<void> otherAsync(File file) async {
await file.readAsString();
await file.length();
await file.delete();
}
Future<void> projectType(Store store) async {
await store.exists();
await store.lastModified();
}
Future<void> dynamicReceiver(dynamic entity) async {
await entity.exists();
}
Future<void> statByPath(String path) async {
await FileStat.stat(path);
}Configure avoid-slow-async-io
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"performance": {
"avoid-slow-async-io": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/performance/avoid-slow-async-io: <reason>More performance rules
- avoid-slow-collection-methods
Reports filtered iterable searches that have a direct search-method alternative.
- avoid-unnecessary-collections
Avoid singleton collection literals where SDK APIs accept a single value.
- avoid-unnecessary-consumer-widgetsriverpod
Flags Riverpod consumer widgets whose build reference is unused.
- avoid-unnecessary-futures
Prefer synchronous declarations when an async Future only returns scalars.
- avoid-unnecessary-gesture-detectorflutter
Flags handler-free Flutter
GestureDetectorsand tap-only wrappers around enabled Flutter buttons. - avoid-unnecessary-hook-widgetsflutter_hooks
Flags confirmed hook widgets and
HookBuildercallbacks that do not use hooks. - move-variable-closer-to-its-usage
Move final/const scalar literals into their only branch, or after an if.
- move-variable-outside-iteration
Reports loop-local final variables whose computation is independent of the loop.