Adding Packages In Flutter App

In this post we will look at the various ways for adding packages in Flutter application.

Introduction

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 in a Flutter application. Mainly, you can add a package from:

  • Flutter pub.dev (official Flutter package server)
  • GitHub
  • Local File System

How To Add Packages From Flutter Pub

Adding Plugin From Pub.Dev
Adding Plugin From Pub.Dev
  • 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 on the image above, just copy and paste the dependency in your pubspec.yaml file.
  • 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/packages from Github server as long as you have access to that repository.

  • Get the url for the remote repository.
  • Add 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 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 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 in 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 in your machine.
  • Use the path keyword to add reference to this plugin.

my_local_package:
path: ../packages/my_local_package/

About the author