Difference between ways to import with and without 'package:'

Asked

Viewed 546 times

3

When we import a class/library two options are suggested, I wonder if there is any difference/advantage between importing classes/libs directly informing the path and with the 'command' package:

Ex:

import 'controller/conferencia_controller.dart';
import 'package:conferencia/controller/conferencia_controller.dart';

2 answers

3


The following import :

import 'controller/conferencia_controller.dart';

Is an import relative. This means that the specified path is relative to the file that is making the import. Even, it is also possible to make Imports using ..which means climbing a directory:

import '../controller/conferencia_controller.dart';

This type of import works, and for small projects should not generate many problems. But if a file changes path, this import will break. This means that you will have to re-look all the sites you made that import and suit the modification. Well, there are FDI’s that do it automatically, and I don’t know how far each can discern.

However, this cannot always be done. When creating a new package (and this includes your own project), the directory /lib is what becomes public to access externally. Normally, in this directory is only what should be visible to those who want to import their package in the future, exporting the necessary files. In addition, it is common to unify all Xports into a single file. Dart, to be only an isolated import, referring to the functionalities of that package.

Directly inside /lib, the library’s main file, Shelf.Dart, exports numerous lib/src files.

(Same website linked up, free translation)

That is, if you want to import a file from within a package, from outside of it, or vice versa, you need to use the second import method (referencing which package it is):

import 'package:utilities/utilities.dart';

This second type of import, allows Imports only of what is public to the package, that is, inside the folder /lib, and regardless of the path relationship between the files.

According to the linked site:

When in Doubt, use the package: Directive; it Works in all cases.

(When in doubt, use import "package:"; works in all cases

Therefore, a healthier convention is to use the second type always. Well, in small projects it shouldn’t hurt much not to do so, since having a sense of what is being done.

1

The first option does not refer to the project root. If the file (which is importing another) where you have this import is moved to another directory, import will no longer work.

Let’s assume a file main.dart which is stored in the root of our project, and in parallel has other files, and a directory called controller/. Inside this directory we have the file that we are importing here:

import 'controller/conferencia_controller.dart';

If in the meantime we decided to change our main.dart to another directory, we would no longer have direct access to the directory controller/ and our import would give error.

However, if we follow the second import option that references the project root (package:conferencia):

import 'package:conferencia/controller/conferencia_controller.dart';

We can change our main.dart for any directory of our project, the reference will always be correct.

  • Opa John, could you explain better? Put an example?

  • I have already edited the answer to clarify and give examples.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.