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.
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.
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!
To use a package in your code, you need to import it. In our example the import command looks like this:
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
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:
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:
SSH access to private repositories is also possible with this approach:
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.