prefer-relative-imports

Same as prefer_relative_imports in Dart lints

Path
lint/correctness/prefer-relative-imports
Default
Off, opt in
Origin
Dart lints
Rule
398 of 512

Prefer relative imports for libraries of the same package under lib/.

The rule runs only for files inside the enclosing package's lib/ directory (the nearest pubspec.yaml names the package; files outside any package, or in a package without a name, are skipped). An import whose URI is a package: URI naming the file's own package, such as import 'package:my_pkg/src/a.dart'; inside my_pkg/lib/, is reported and should be written relative (import 'src/a.dart';). Imports of other packages, dart: libraries, exports, and conditional alternatives are not checked. Incompatible with always-use-package-imports.

Invalid

lib/bad_imports.dartdart
import 'package:app/src/a.dart';
import 'package:app/b.dart' as b;
import 'package:app/src/c.dart' show C;
import 'package:app/src/d.dart' deferred as d;
import "package:app/e.dart" hide E;
import 'package:app/src/f.dart'
    if (dart.library.io) 'src/f_io.dart';

void main() {}
lib/bad_imports.dartUse relative imports for files in the `lib` directory.
1import 'package:app/src/a.dart';
lib/bad_imports.dartUse relative imports for files in the `lib` directory.
1import 'package:app/src/a.dart';
2import 'package:app/b.dart' as b;
lib/bad_imports.dartUse relative imports for files in the `lib` directory.
2import 'package:app/b.dart' as b;
3import 'package:app/src/c.dart' show C;
lib/bad_imports.dartUse relative imports for files in the `lib` directory.
3import 'package:app/src/c.dart' show C;
4import 'package:app/src/d.dart' deferred as d;
lib/bad_imports.dartUse relative imports for files in the `lib` directory.
4import 'package:app/src/d.dart' deferred as d;
5import "package:app/e.dart" hide E;
lib/bad_imports.dartUse relative imports for files in the `lib` directory.
5import "package:app/e.dart" hide E;
6import 'package:app/src/f.dart'

Valid

example.dartdart
import 'dart:async';
import 'src/a.dart';
import '../b.dart' as b;
import 'package:other/other.dart';
import 'package:collection/collection.dart' show IterableExtension;

void main() {}

Configure prefer-relative-imports

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "prefer-relative-imports": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/correctness/prefer-relative-imports: <reason>
  • prefer-sealed-bloc-eventsrecommendedbloc

    Flags Bloc event classes without a sealed or final modifier.

  • prefer-sealed-bloc-staterecommendedbloc

    Flags Bloc state classes without a sealed or final modifier.

  • prefer-void-to-nullprefer_void_to_nullrecommended

    Flags uses of the Null type where void is meant.

  • proper-controller-disposeproper_controller_disposerecommendedflutter

    Requires Flutter controllers owned by a State field to be disposed.

  • proper-expanded-and-flexibleproper_expanded_and_flexiblerecommendedflutter

    Require Expanded and Flexible to sit directly inside a flex widget.

  • proper-from-environmentproper_from_environmentrecommended

    Require fromEnvironment to be evaluated in a const context.

  • proper-super-init-stateproper_super_init_staterecommendedflutter

    Require super.initState() to be the first statement of a resolved Flutter State subclass's initState override.

  • require-super-disposerecommendedflutter

    Require super.dispose() in Flutter State dispose overrides.