Flutter imports : relative path or package?

In Flutter, for importing libraries within our own package's lib directory, should we use relative imports

import 'foo.dart'

or package import?

import 'package:my_app/lib/src/foo.dart'

Dart guidelines advocate to use relative imports :

PREFER relative paths when importing libraries within your own package’s lib directory.

whereas Provider package says to always use packages imports :

  • Always use package imports. Ex: import 'package:my_app/my_code.dart';

Is there a difference other than conciseness? Why would packages imports would reduce errors over relative imports?


From the same Dart guidelines, further down they give this reason for the relative imports:

There is no profound reason to prefer the former—it’s just shorter, and we want to be consistent.

Personally, I prefer the absolute method, despite it being more verbose, as it means when I'm importing from different dart files (in other folders), I don't have to work out where the file to be imported is, relative to the current file. Made-up example:

I have two dart files, at different folder levels, that need to import themes/style.dart:

One is widgets/animation/box_anim.dart, where the relative path import would be:

import '../../themes/style.dart';

The other is screens/home_screen.dart with the relative import:

import '../themes/style.dart';

This can get confusing, so I find it better to just use the absolute in both files, keeping it consistent:

import 'package:myapp/themes/style.dart';

And just stick that rule throughout. So, basically, whatever method you use - Consistency is key!

The Linter for Dart package, also has something to say about this, but is more about the Don'ts of mixing in the '/lib' folder:

DO avoid relative imports for files in lib/.

When mixing relative and absolute imports it's possible to create confusion where the same member gets imported in two different ways. An easy way to avoid that is to ensure you have no relative imports that include lib/ in their paths.


TLDR; Choose the one you prefer, note that prefer_relative_imports is recommended in official Effective Dart guide

First of all, as mentioned in this answer, Provider do not recommands package imports anymore.

Dart linter provides a list of rules, including some predefined rulesets :

  • pedantic for rules enforced internally at Google
  • lints or even flutter_lints (previously effective_dart) for rules corresponding to the Effective Dart style guide
  • flutter for rules used in flutter analyze

Imports rules

There is actually more than two opposites rules concerning imports :

  • avoid_relative_lib_imports, enabled in pedantic and lints rulesets, basically recommend to avoid imports that have 'lib' in their paths.

The two following are the one you mention :

  • prefer_relative_imports, enabled in no predefined rulesets, but recommended in Effective Dart guide in opposition to :

  • always_use_package_imports, enabled in no predefined rulesets. Which means that it is up to you and to your preferences to enable it (be careful, it is incompatible with the previous rule)

Which one should I choose?

Choose the rule you want ! It will not cause any performance issue, and no rule would reduce errors over the other. Just pick one and make your imports consistent across all your project, thanks to Dart linter.

I personnaly prefer using prefer_relative_imports, as it is recommended by Dart team, with this VSCode extension which automatically fix and sort my imports.