skip to Main Content

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


  1. 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:

    library my_library
    export 'package:ffmpeg_kit_flutter/ffmpeg_kit.dart';
    

    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 running pub get, however it will be less of a hassle compared to adding the dependencies manually.

    Hope this answers your question.

    Login or Signup to reply.
  2. 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 states

    Depending explicitly on packages that you reference ensures they will always exist

    This 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:

    • you’d need to create the base project
        .
        ├── base_pkg
        │   ├── lib
        │   │   └── base_pkg.dart
        │   └── pubspec.yaml
        └── working_project
            ├── ...
            └── pubspec.yaml
      
    • add the dependencies to the pubspec.yaml
      # (base_pkg) pubspec.yaml
      name: base_pkg
      
      dependencies:
         json_annotation: ...
      
    • export the dependencies from the project’s src file
      // lib/base_pkg.dart
      
      export 'package:json_annotation/json_annotation.dart';
      
    • add the base project to your working project (via path)
      # (project) pubspec.yaml
      
      dependencies:
         base_pkg:
           path: ../base_pkg # absolute/relative path to base_pkg
      
    • use the base_pkg’s dependencies within your project
      import 'package:base_pkg/base_pkg.dart';
      
      @JsonSerializable()
      class SomeClass {
          ...
      }
      

    Note:
    This approach will only work with dependencies not dev_dependencies. All dev_dependencies are only used within the project that imports them, so you would need to re-add them to your working project.

    Dev dependencies differ from regular dependencies in that dev dependencies of packages you depend on are ignored.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search