Skip to content

Instantly share code, notes, and snippets.

@catalunha
Last active January 23, 2024 21:49
Show Gist options
  • Save catalunha/5ca2df1084b01f8a859733d8c0969737 to your computer and use it in GitHub Desktop.
Save catalunha/5ca2df1084b01f8a859733d8c0969737 to your computer and use it in GitHub Desktop.
about Relative vs Package imports in Flutter and Dart

About Relative vs Package imports in Flutter and Dart

What the documentation says:

https://dart.dev/tools/linter-rules/prefer_relative_imports

and

https://dart.dev/tools/linter-rules/avoid_relative_lib_imports

The orientation of both seems to converge to avoid confusion between external packages and the one under development. Since everything in dart is a package.

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.

When mixing relative and absolute imports it’s possible to create confusion where the same member gets imported in two different ways. One way to avoid that is to ensure you consistently use relative imports for files within the lib/ directory.

This is why I use relative (for paths in the package under development) and absolute (for packages imported into pubspec).

Using Relative import is strongly recommended by the community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment