1

I have flutter project with tree looks like

...
├── lib
│   ├── UI
│   │   ├── base_widget.dart
│   │   ├── components
│   │   │   ├── card.dart
│   │   │   ├── centered_progressIndicator.dart
│   │   │   ├── gradient_button.dart
│   │   │   └── rounded_network_image.dart
│   │   ├── more
│   │   │   ├── announcements
│   │   │   │   └── announcements.dart
│   │   │   ├── more.dart
│   ├── main.dart
├── pubspec.lock
├── pubspec.yaml
└── test
    └── widget_test.dart

In announcements.dart(also in some other files) I want to import card.dart. Now I am importing like import '../../components/card.dart';.

If I change name of components directory to something else, I have to edit all the files where I import components/card.dart.

Also, I know relative import with ../ is not recommended, should use package:.

My question is how to write an import statement that doesn't affect directory structure changes? Is this possible in dart?

Johnykutty
  • 12,091
  • 13
  • 59
  • 100

3 Answers3

3

You should definitely be able to use relative imports inside your own package (and never into or out of a package).

That is, I completely disagree with:

Also, I know relative import with ../ is not recommended, should use package:.

If you rename a directory containing many files, then all references to those files need to be rewritten. There is no way around that. If you use relative paths between the files, then the files inside the renamed directory which refer to each other, will not need to change because their import paths won't contain the changed name. If you use package: URIs everywhere, those will also need to be changed.

Any package: references outside of the package, which targets a file in the renamed directory, will also need changing, so if the renamed directory is not under lib/src/, and therefore not intended for direct external access, then renaming is a breaking change. That is why it's recommended that you put most of your source files into lib/src/ and then reexport them from fixed libraries placed irectly in lib/. That makes it a non-breaking change to restructure the files under lib/src/, as long as you export the same declarations in the public libraries.

Such a rename is a large refactoring, and having an IDE help you rename all references is a good idea, if it supports it.

lrn
  • 64,680
  • 7
  • 105
  • 121
1

package: imports are more robust to changes than relative imports, but you'll need assistance from your editor or IDE to rename a library file/directory. In VS Code, you can use ctrl+shift+h to open a project-wide string replace. This is a common feature of editors, and is likely the best way to change your import statements after renaming a library file.

If you are using VS Code, there is also dart-import, an extension for changing all of your imports to the package-relative syntax, and can also remove unused imports.

Jacob Phillips
  • 8,841
  • 3
  • 51
  • 66
0

Use can use package import.In ".packages" file inside your project add "projectname/lib". Then you can do following "import package:projectname/components/cards"

Path added in .packages will give relative path to lib folder of project.

Bikash Maharjan
  • 163
  • 1
  • 1
  • 4