Advanced Usage of Dependencies in Flutter

Purusothaman Ramanujam
What The Flutter?
Published in
2 min readNov 21, 2018

The pubspec.yaml file is the most important file in any Flutter project. It is the place where you provide all the required dependencies of your Flutter project. Knowing how to use it better can help us in many unexpected ways.

It’s straightforward to use a public package in your pubspec.yaml file. But what if you want to use some packages that are not yet published in https://pub.dartlang.org/. Or you have developed a plugin and want to use this private plugin in your project.

No worries. You can still use them in your next great app.

Using a package/plugin from local directory

Assume that you want to use a local Flutter package or a plugin in another Flutter application. In this case, you can refer the local directory in your pubspec.yaml as shown below.

In the below case “my_package” is the local package that I have in my local machine.

dependencies:
flutter:
sdk: flutter
my_package:
path: ./my_path/my_package

Using a Git based dependency

You can use a package as a dependency which is in a Git repository by specifying its repo URL. It comes handy when the recent changes are pushed to the Git repository and not yet published to the Dart Pub. You don’t need to wait for those changes to be available in Pub. Why to stop our development sprint and spirit?

dependencies:
launch_review:
git:
url: https://github.com/Purus/launch_review

The above examples makes assumption that the root of the Git repository in the package that you need. But in some cases, the package that you need might be in a sub-directory of the Git repository. For such cases, you can refer to the particular path too as shown below.

cloud_firestore:
git:
url: https://github.com/theshiftstudio/flutter_plugins.git
path: packages/cloud_firestore

In the example provided, the Git repository ‘flutter_plugins’ contains all the plugin under root. Since we want to use only the “cloud_firestore” plugin, we can refer that using the “path” configuration.

You can even point to a particular commit reference if you want.

cloud_firestore:
git:
url: https://github.com/theshiftstudio/flutter_plugins.git
ref: 9cabcc57e63fbc9be4fafee08e6435a1a8c71d61
path: packages/cloud_firestore

Earlier I created an issue(https://github.com/flutter/flutter/issues/15252) which arises when using a Git depedency. It is now resolved and we are good to go.

Dependency Versioning

Apart from these, it is super helpful to understand the versioning syntax of the dependencies.

  1. package_name: any
    Allows any version. As per official docs, it is not recommend for performance reasons.
  2. package_name: 1.2.3
    Uses the exact version specified.
  3. package_name > 1.2.3
    Uses any greater version but not the version itself. If not you can use

There are few other variations like using caret notations. For more detailed usages, you can refer to the official docs.

If you like the post, please leave your thoughts in the comments section.

--

--