Can I have my pubspec.yaml use another like a base for the dependencies
to be used in my package?
So pubspec.yaml that can reference another as its base?
Can I have my pubspec.yaml use another like a base for the dependencies
to be used in my package?
So pubspec.yaml that can reference another as its base?
2
Answers
If what you want is for those packages to use the same dependencies, then maybe you could just create a new package where it stores all the needed packages, then create a library. You create a new dart file and paste something like this:
Then for every of the projects which needed all of those packages can just add this new package and import the library file.
So if you for example wanted to add a new dependency to this new exporting package, you can just add it then export it. If you published this exporting package to
pub.dev
you may need to update the version in each of the pubspec.yaml of the projects using the package by just changing the version number and runningpub get
, however it will be less of a hassle compared to adding the dependencies manually.Hope this answers your question.
This wouldn’t be considered a good practice. When you add a dependency (lets call it "Package A") to your project, it becomes a direct dependency, while any of the dependencies of "Package A" become transitive dependencies.
As far as how the two types of dependencies are handled/tree shaken, I don’t know how they differ, but they have specific naming for them 🤷♂️
There is a lint that discourages this type of behavior as well,
depend_on_referenced_packages
. Here is statesThis implies that the dependency could not exist at some point in the future. Probably if some OS maintainer removed a package that they no longer used within their project.
Now, if you’re the maintainer of said package, I am sure that you’ll know whether a dependency has been removed, so this will likely not be an issue.
Ultimately, its up to you, just know that its better practice to explicitly depend on packages that you’re using within your project.
If you wanted to achieve a "base pubspec.yaml", here is how you could do it:
Note:
This approach will only work with
dependencies
notdev_dependencies
. Alldev_dependencies
are only used within the project that imports them, so you would need to re-add them to your working project.