pubspec use dependencies

Flutter tips Published on

Your pubspec.yaml file is like your project's recipe book. It lists all the external packages (libraries) your app needs to work.

How to Add Packages

Import Libraries

You can add libraries from different places. You might have a library in another folder on your computer (local path) or one hosted online, like on GitHub (remote repository).

Here’s how you'd list them:

dependencies:
  transmogrify:
    path: ../
  otherlib:
    git:
      url: https://github.com/awesomeplugin.git
      ref: main

Understand Carret Syntax

When you add a package from pub.dev, you'll often see a little ^ symbol before the version number, like ^1.0.0. This is called the Carret syntax.

What does ^1.0.0 mean?

It tells your project that it can use any version of the package that is backward compatible with version 1.0.0. This usually means any version from 1.0.0 up to (but not including) 2.0.0.

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.0

This is the same as >=1.0.0 <2.0.0.

Handling Version Issues

Dependency Overrides

Sometimes, different packages in your project might need different versions of the same underlying library. This can cause conflicts. Dependency overrides let you tell your project exactly which version of a library to use, no matter what other packages ask for.

This helps avoid problems when multiple parts of your app rely on the same thing but expect different versions. You are temporarily forcing all references to use a specific version.

name: my_app
dependencies:
  transmogrify: 3.2.1
  otherlib: ^1.0.2
dependency_overrides:
  transmogrify: 3.2.1

In this example, even if otherlib normally uses transmogrify version 2.0.0, the dependency_overrides makes sure the project uses version 3.2.1 for transmogrify everywhere.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Where do I list the packages my Flutter app uses?

You list all required packages under the `dependencies:` section in your `pubspec.yaml` file.

What does the `^` symbol mean in a dependency version?

The `^` symbol (Carret syntax) means your project can use any version of the package that is compatible with the specified version, until the next major version increment.

How can I add a package located in another folder on my computer?

You can use the `path:` option under the package name in your `dependencies:` list.

How do I force my project to use a specific version of a dependency if there are conflicts?

You can use the `dependency_overrides:` section in your `pubspec.yaml` file to specify the exact version to use.

Read more
You may also be interested in
Flutter tips: some useful date extension methods  blog card image
Flutter tips: some useful date extension methods
Published on 2025-05-04
Smart asking for rating  blog card image
Smart asking for rating
Published on 2025-04-28
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved