Skip to content

Add lint to prefer absolute imports #58749

@vaetas

Description

@vaetas

Describe the rule you'd like to see implemented

I would like to request a lint rule to prefer absolute imports over relative & package imports for local source files.

Lints like always_use_package_imports and avoid_relative_lib_imports currently do not permit absolute imports.

I wonder if this lint would be technically possible, or if there is a reason why this is not allowed.

Examples

// GOOD:
import 'package:flutter/material.dart';

import '/screen/home_screen.dart';
import '/screen/about_screen.dart';

// BAD:
import 'package:flutter/material.dart';
import 'package:example/screen/about_screen.dart';
import 'package:example/screen/home_screen.dart';

// BAD:
import 'package:flutter/material.dart';

import '../../../about_screen.dart';
import '../../../screen/home_screen.dart';

Context

In our recent projects we have used the absolute import for local source files: import '/a/b.dart';. I like this solution as it clearly separates local source code and third party libraries. Main improvement over the package import is that the absolute import will not change across the project. Also, with absolute imports you avoid the ugly ../../../b.dart.

Android Studio allows to organize imports on save. By default, it makes empty line between package and other imports. In my opinion this again improves readability and works well with absolute imports.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposaltype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions