Package dependencies – pub

Now that you understand the most important role of the pubspec file in the package when developing Dart applications, you can add third-party package dependencies to your project. There are important pub commands that you can work with when adding or updating package dependencies to your project. We also need to demonstrate how to properly specify the dependency version that we are required to use.

After you start a new Dart project, either manually or by using a generator tool such as Stagehand, the first thing you must do is run the following command:

 pub get 

For example, the following package contains only the following pubspec file:

Additionally, it contains the pubspec contents, as follows: 

name: adding_dependencies

This is a minimal package description and it does not have any dependencies specified, not even the target Dart SDK version. However, let's execute the pub get command inside the package folder, as it will work in the same way:

pub get

We get the following successful output:

Resolving dependencies...
Got dependencies!

We will get a file structure like in the following screenshot:

Notice the new files generated by the command inside the .packages folder; these files are important for the pub tool to work with the dependency packages:

  • .packages: This maps the dependencies in the system's pub cache (previously mentioned in the Stagehand – the Dart project generator section). Instead of making copies in all of your packages, the pub tool simply stores the mapping between the package and its respective location in the system. After the package is mapped here, it will be available for you to import inside your Dart code. This file should not be included under the source code management system; this is because it's generated and managed by the pub tool. 
  • pubspec.lock: This is the auxiliary file to the pub tool that contains all of the dependency graphs of the package, that is, it lists all of the immediate dependencies and the transitive ones. It also contains the exact versions and other metadata information about all of the dependencies. It is recommended that you include this file in the source management system only if it is an application's package; this helps a dev team, for example, to work with the exact same dependency configuration. If you are using a library package, then it's typically not included, as it is expected to work with a large range of dependencies, that is, it should not be locked to specific versions. 
Remember, this is all made by the pub tool, so you should not touch these files. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.147.124