theProCoder
theProCoder

Reputation: 435

Change VS Code to auto-import files in flutter relatively

So, here is the problem:

Whenever I am coding with Flutter and Dart, and I use a feature from another file, when I press Tab, VS Code will auto-import the file for me.

The thing is, VS Code will use the package import syntax, like import 'package:<project_name>/<file_path>';. But I prefer relative import syntax.

So, is there a setting I can change in VS Code for Dart to configure it to use relative imports by default?

Thanks!

Upvotes: 11

Views: 7550

Answers (3)

Yam Horesh
Yam Horesh

Reputation: 192

It isn't necessary to download an extension. As explained here, simply add this to the analysis_options.yaml file:

linter:
  rules:
    - prefer_relative_imports

Upvotes: 6

Arkar Min Tun
Arkar Min Tun

Reputation: 655

Personally, I am using dart-import extension. There is an extension setting you can turn on to modify imports on save. So, you can have a consistent import structure without needing to choose every single time.

dart import fix on save

There is a recommendation in effective dart guideline for relative path import usage.

Upvotes: 6

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63709

While importing, you can find both option by tapping on yellow bulb or pressing ctrl+.

enter image description here

Another handy thing, using dart-import extension on Vs-code.

Upvotes: 9

Related Questions