To add a new package to a Flutter application, we need to add a reference to it as a dependency in the pubspec.yaml file. There are different ways to add packages to a Flutter application. Mainly, you can add a package from:
- Flutter pub.dev (official Flutter package server)
- Local File System
How To Add Packages From Flutter Pub
- Go to the official site (pub.dev) for Flutter packages and find the plugin that you want to install.
- On the “Installing” tab you can find the complete instruction on how to install the plugin.
- As shown in the image above, just copy and paste the dependency in your
pubspec.yamlfile. When you save the YAML file, it will automatically download the package into your machine.
How To Add Flutter Packages From GitHub
You can use any Flutter plugin/package from the Github server as long as you have access to that repository.
- Get the URL for the remote repository.
- Add a reference to the plugin repository in the pubspec file as below:
useful_service: git: https://github.com/xyz/useful_service.git
- You can also specify a specific branch of the repository as well by specifying the branch name with ref keyword.
useful_service: git: url: https://github.com/xyz/useful_service.git ref: develop
How To Add Package From Local File System In Flutter
You can add a reference to a plugin/package directly from your local computer. Just be careful when you share the project with your team. Since the plugin is only on your computer, others will not be able to use the plugin when working on the same project.
- Get the relative path to the folder where your plugin is located on your machine.
- Use the path keyword to add a reference to this plugin.
my_local_package: path: ../packages/my_local_package/