Dart Pub Package Manager & Dependency Configuration

Day 3 of my 100-day Flutter learning journey This blog will cover my understanding of what I have learned thus far. Let us get in.

As I stated in my last post, a Dart package is a collection of code that other projects can reuse for different purposes. And I spoke about the Dart package manager depending on a pubspec.yaml file to manage your Dart project dependencies. I will write more on the pubspec.yaml file

The Pubspec.yaml File

The pubspec.yaml file is essential to your Dart package or project. This is because your dart project is nothing more than a folder containing other folders and files without this file. Why so? This file contains, at a minimum, the name and the SDK constraints your project will be bound by. And without it, you can't depend on other packages, and vice versa, since this file is where you state your project's dependencies and additional information needed to publish your project to pub.dev.

Another reason why this file is necessary to be able to publish your package is that it provides other projects with information about the packages your project depends on since your dependencies will now be acting as transitive dependencies to those projects. It prompts me to discuss the various types of dependencies.

THE 4 TYPES OF DEPENDENCIES

  1. Immediate Dependencies - These are the dependencies your project directly depends on

  2. Transitive Dependencies - These are dependencies on which your project's dependencies depend. Let’s say you board a busan immediate dependency to workyour project every morning, but before the bus reaches your location, it uses a ferry to access the road to your place. In this case, the ferry becomes your work’s transitive dependency.

  3. Regular Dependencies - The dependencies your project uses in both the development and production phase of your life.

  4. Dev Dependencies - Dependencies used only in the development phase of your project.

How Are Dependencies Added To Your Project?

For every dependency you add to your pubspec.yaml file, you must state the package name and the range of versions of the package you allow in your app.

Not all dependencies or packages are added from pub.dev. You can depend on packages from other sources too. Let’s look at four other sources to get your packages from;

Hosted Packages: these are packages hosted on other HTTP servers that communicate the same API as pub.dev

GIT Packages: allow you to work on the most recent versions of packages that have not yet been officially released.

Path Packages: These are other small local packages that your app depends on. These packages are typically used in large applications.

SDK Packages: These are for SDKs shipped with packages, and the SDKs may be dependencies themselves. Flutter is the only SDK supported in Dart in this instance.

Packages added to your project ought to be managed, i.e. installed and kept up to date; the manager is called the Pub Package Manager.

DART PUB PACKAGE MANAGER

The Dart Pub package manager is also one of the tools in the Dart SDK. It provides a command-line interface for managing various packages in a Dart project. The pub package manager communicates with the pubspec.yaml file to know the dependencies and libraries needed for your project to function.

When you run your project, the package manager will automatically download and install the required packages (and their dependencies) from the official Dart package repository or the source from which you are getting your package.

The package manager provides several command-line tools, such as pub get and pub upgrade, that you can use to manage your project's dependencies. These tools allow you to add new packages to your project, update existing packages, and remove packages you no longer need. Here are two Pub command lines, and their functions

Dart pub get retrieves the packages listed as your project's dependencies. After Pub does this, it generates files into a cache directory on your PC and maintains it.

Dart pub upgrade retrieves the latest version of each package listed as dependencies used by the current package