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.