An Introduction to Packages

Like many languages, Dart allows developers to share modular code, which can be used by other developers to easily get commonly needed functionality that would otherwise require a complicated solution.

There are many generic Dart packages we can use to achieve functionality that is useful to both web applications and Flutter mobile apps, but there are also Flutter-specific packages, which use Flutter-specific features and only work with Flutter apps.

Generally, packages are written in Dart and are very similar to other languages’ packages and libraries, but can communicate with device-specific Android/iOS code, which would make them plugin packages.

Finding Packages: Using Dart Pub

You can find packages by going to the Dart Pub homepage[27] and searching in the “Flutter” and “All” categories.

In the home page you will find the most commonly used packages, many of which are made by Google developers. Clicking on a package allows you to find usage information, how to install the package and a changelog.

Installing Packages

Once you’ve found a package you think you might want, you need to install it.

Dart packages get installed when they are specified as a dependency of an app, and you can do this by editing the pubspec.yaml file.

The Simple Way

Installing a package is very easy: you just need to edit the dependencies section of the pubspec.yaml file and add the string from the Installing section of the package in Pub. The strings you find there look like this:

 packagename: ​^version

And need to be added in pubspec.yaml, indented on the same level as the flutter: line, after the lines that look like the following:

 dependencies:
  flutter:
  sdk: ​flutter

The IDE plugins download the packages automatically when you edit the file, but you can make Flutter download or update the dependencies by running the following command:

 $ flutter packages get

All that’s left to do is to import the package in your Dart file by adding, at the top of the file, the following line:

 import​ ​'package:packagename/filename.dart'​;

The file name is the same as the package name in most cases, but it’s better to check on Pub, since it’s usually specified in the README.md, Example, or Installing sections. If you’re using the official IDE plugins the correct file name will also be suggested by the IDE.

import also has more features, which are explained in Mastering import.

That’s all you need to know if the package developers are using semantic versioning correctly and you just want to get all of the updates for the package that won’t break your code, but there are cases in which simply copying and pasting from Pub to the dependencies isn’t ideal or doesn’t work at all.

Specifying the Package Version Manually

Understanding how to specify dependencies manually could be useful, for example, when you want to update to the next version, even if you need to change your code.

It could also be useful when you want a specific package version or always the latest package version, like would happen when you are the package developer.

Caret Syntax

The line we saw before declares that the project is dependent on the latest version of the package from Pub that is declared to be compatible with the version specified. For example, ^0.1.0 means any version in the 0.1 group, and ^1.2.3 means any version greater than or equal to 1.2.3 and less than 2.0.0. This is the most common way to specify the package version, and it is called caret syntax .

Other Ways to Specify the Package Version

The package versions can also be specified using mathematical comparison operators (for example, >=0.5.9 will use any version starting from 0.5.9).

These operators can also be combined (for example, you can use something like >=0.5.9 <2.5.3).

You can also use just:

 packagename:

or:

 packagename: ​any

if you want Flutter to download the latest version it can find, with no restrictions.

Fetching Packages from Locations Other Than Pub

The version isn’t the only thing that you can specify in the dependencies list: if you don’t want the package to be fetched from Pub, you can also specify where you want the package to be fetched from, which you might find useful, for example, when testing a new version that hasn’t yet been published or when using a package that isn’t on Pub at all.

Fetching from Git

For example, you can fetch the package from a Git repository:

 packagename:
  git:
  url: ​git://github.com/username/packagename.git

If the package isn’t in the root of the Git repository, you can specify a path within the repository where the package is located:

 packagename:
  git:
  url: ​git://github.com/username/packagename.git
  path: ​path/to/pkg

Fetching from a Local Path

You can also fetch the package from a path in your local development environment:

 packagename:
  path: ​../packagename/
..................Content has been hidden....................

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