avoid-slow-async-io

Same as avoid_slow_async_io in Dart lints

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

example.dartdart
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');
  }
}
Avoid using asynchronous `dart:io` methods here.
3Future<void> fileQueries(File file) async {
4 await file.lastModified();
Avoid using asynchronous `dart:io` methods here.
4 await file.lastModified();
5 await file.exists();
Avoid using asynchronous `dart:io` methods here.
5 await file.exists();
6 await file.stat();
Avoid using asynchronous `dart:io` methods here.
9Future<void> directoryQueries(Directory dir) async {
10 await dir.exists();
Avoid using asynchronous `dart:io` methods here.
10 await dir.exists();
11 await dir.stat();
Avoid using asynchronous `dart:io` methods here.
14Future<void> linkQueries(Link link) async {
15 await link.exists();
Avoid using asynchronous `dart:io` methods here.
18Future<void> staticQueries(String path) async {
19 await FileSystemEntity.isDirectory(path);
Avoid using asynchronous `dart:io` methods here.
19 await FileSystemEntity.isDirectory(path);
20 await FileSystemEntity.isFile(path);
Avoid using asynchronous `dart:io` methods here.
20 await FileSystemEntity.isFile(path);
21 await FileSystemEntity.isLink(path);
Avoid using asynchronous `dart:io` methods here.
21 await FileSystemEntity.isLink(path);
22 await FileSystemEntity.type(path);
Avoid using asynchronous `dart:io` methods here.
26 final file = File('data.txt');
27 if (await file.exists()) {

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "performance": {
        "avoid-slow-async-io": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/performance/avoid-slow-async-io: <reason>