2

Effective dart says

"DON’T import libraries that are inside the src directory of another package."

reason being it breaks abstraction and could potentially break your app if the library was to change its underlying implementation.

They do not provide an alternative or a solution.

Im currently working on a dart package that my app depends on. What would be the correct way to import the models or classes from it instead of importing it directly from package src folder?

jia chen
  • 710
  • 1
  • 6
  • 16
  • There is no correct way if the classes are not exported from files outside `src/`. – Günter Zöchbauer Dec 04 '18 at 16:01
  • so will just putting files in a dir on the same level as the src folder be the solution? can that be seen as a commitment not to break or change these classes? – jia chen Dec 04 '18 at 19:06
  • Sorry, I don't understand what you mean. Did you develop the package where you want to import from, or is it 3rd-party? – Günter Zöchbauer Dec 04 '18 at 19:08
  • both are mine, the package and the app importing the package. – jia chen Dec 04 '18 at 19:31
  • 3
    Then add a file `lib/my_package.dart` and export from there what you want to have available for consumers of your package. Like `export 'src/file1.dart';` or ``export 'src/file1.dart' show Class1 hide Class2;` – Günter Zöchbauer Dec 04 '18 at 19:34

1 Answers1

0

As previously mentioned in the comments, you can define which classes you can expose from your package. You're correct that it's recommended to avoid exposing more API than intended - it's also mentioned in the docs. To do this, you can define the exposed classes:

export 'src/your_file.dart' show ExposedClass hide HiddenClass;
Omatt
  • 8,564
  • 2
  • 42
  • 144