How To Install Packages In Your Flutter App

Flutter category image

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.

Install packages from 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.

With the 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.

Edit the pubspec.yaml file

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:

import 'package:url_launcher/url_launcher.dart';

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

Reference packages from 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:

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

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

Reference packages from 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:

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:

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:

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

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.

Related articles