How To Install Packages In Your Flutter App

Flutter category image

Introduction

This short article shows how to install packages in your Flutter app so that you can use existing code and you don’t have to write everything yourself.

With packages, the development of apps can be accelerated and simplified by a lot. We have a look at how to find packages, how to add them to our app, and how we can use them in the code. For demonstration purposes, I am going to use the package url_launcher in this example. Here is how to install packages in your Flutter app.

Find packages

Most Flutter and Dart packages are hosted in the official package repository on pub.dev. You can use it to get detailed information about a package, a usage example, and installing guidelines.

A collection of famous and good packages is Flutter Awesome. If you need inspiration on what packages are out there, check out this website.

✅ Did you find a package that you want to use? Then let’s install it together in the next part.

Packages on pub.dev

There are two ways to install packages in your app. Either by using the command-line interface or by manually adding entries to your pubspec.yaml file.

Install with Flutter CLI

Navigate to the root folder of your project and execute the command flutter pub add url_launcher. This will add a new entry in your pubspec.yaml file with the most recent version of the package available. Furthermore, it will download the sources and make the package ready for use.

Install manually

Add a new entry in your pubspec.yaml file under the dependencies node with the name of the package and a version specifier like this: url_launcher: ^6.0.20. You can find the most recent version on the package’s page on pub.dev.

Package url_launcher in pubspec.yaml
Package url_launcher in pubspec.yaml

Afterward, you need to execute the command flutter pub get to make the package ready for use. Some IDEs like Visual Studio Code will automatically execute the command when the pubspec.yaml file has changed.

✅ Is your package installed? Great! Then let’s use it in the code!

Use packages

To use a package in your code, you need to import it. In our example the import command looks like this:

Dart
import 'package:url_launcher/url_launcher.dart';

For usage instructions, check the example tab on pub.dev for your package.

Packages on your local machine

There are cases when you need to reference a package from your local file system. With the following example, you can include it in your pubspec.yaml:

YAML
dependencies:
  library_name:
    path: /path/to/library_name

The path needs to point to the root folder of the library!

Packages in a GIT repository

Let’s assume, your package is not hosted on pub.dev but in a GIT repository like GitHub, Bitbucket, or others. Then, you can reference it in your pubspec.yaml with the following lines:

YAML
dependencies:
  library_name:
    git:
      url: https://github.com/user/library_name.git
      ref: main

This will reference the main branch of the repository. You can also use other branch names, commit ids, or tags.

If the repository is private and you have an access token, use the following schema:

YAML
dependencies:
  library_name:
    git:
      url: https://<access-token>@github.com/user/library_name.git
      ref: main

SSH access to private repositories is also possible with this approach:

YAML
dependencies:
  library_name:
    git:
      url: git@github.com:user/library_name.git

Packages in a custom repository

If your package lies in a custom repository, you can also include it in your Flutter apps. Reference it in your pubspec.yaml like this:

YAML
dependencies:
  library_name:
    hosted: <repo-url>
    version: ^1.11.9

Usually, you have a token to authenticate with your repository. Run the command dart pub token add <repo-url> and then enter the token in the next step. From now on, the tooling will use this token whenever it needs to access the given repository.

A major benefit of custom repositories is that you can specify versions that you want to use just like with official package repository pub.dev.

Conclusion

With this guide, you should know how to install packages in your Flutter app. For more information about packages go to the official Flutter documentation.


Want More Flutter Content?

Join my bi-weekly newsletter that delivers small Flutter portions right in your inbox. A title, an abstract, a link, and you decide if you want to dive in!

Become A Firebase Expert!

Take a deep dive into Firebase and learn how to really handle their services. This ebook makes you an instant Firebase professional!

Flutter ❤️ Firebase

Get started with Firebase and learn how to use it in your Flutter apps. My detailed ebook delivers you everything you need to know! Flutter and Firebase are a perfect match!