Pub is a package management tool that makes it easy for you to install, use, share Dart libraries, command-line tools and other assets. It comes as part of the Dart SDK, and requires a dependency file called pubspec.yaml.
Instructor: [00:00] Dart manages packages using a tool called Pub. To use packages in your project, create a pubspec.yaml file at your root directory. The simplest detail you can add to your pubspec file is the package name. Optionally, you can add in a description, version, and author.
[00:14] To pull in external packages, define the dependencies key, followed by a list of dependencies. Running the pub get command reads the dependencies listed in the pubspec file and installs them in a central location on the operating system. You can also specify a package version by defining a version constraint.
[00:35] A .packages file is generated with details of the packages installed and the allocation on the system. This file is not to be committed to version control, as the system cache location differs depending on what operating system you are installing your packages.
[00:49] Dart package versions use the semantic versioning format. So far, the package version is set as concrete, which means that exact version is pulled into our project. Using the greater than equals symbol, followed by the version number, allows the provided version or any greater one.
[01:07] Using just the greater than symbol, followed by the version number, allows any greater version, excluding the given version number. Running pub get throws an exception, because the path module has no greater version than 1.6.2 at the time of this recording.
[01:23] Using the less than equals allows any version less than or equal to the provided version. Removing the equals symbol allows any lesser version, excluding the provided version number. Now, because the current Dart version at the time of this recording is over 2.0there is no version of the path package below 1.6.2, which supports the current SDK version.
[01:46] Let's comment that line out and try again. We can also specify a range of version. Here, we are saying that we want a version greater than or equal to the provided version, while excluding the next major version, 2.0In fact, we can simplify this by using the caret symbol. Set the environment details to ensure the package pulled in is compatible with the given Dart SDK version constraint. You can define dev dependencies for packages that are only used during local development.
[02:25] To work with installed packages, use the import syntax, followed by the package prefix and name. The Dart runtime will look up everything after the package prefix in the .packages file. Let's run this Dart file.
[02:41] Use the as keyword, followed by a key to create the namespace for the imported package. This style can also be used to import files from the current project. To demonstrate that, let's create a file in the lib folder and define a function to return the current timestamp.
[03:03] Returning to main.dart after the package prefix, use the package name you specified in your pubspec file, which will point to the files and folders in your lib directory. We should now be able to invoke our top level function.
[03:19] To upgrade your dependencies to the latest version, run pub upgrade. You can also upgrade a specific package by running pub upgrade, followed by the name of the package.
Thanks for your feedback Daniele.
You're blowing my mind with Dart it's awesome, thank you for these videos!
very well exposed, thank you!