In this tutorial, we're going to learn how to manage dependencies in our Flutter project. This involves keeping track of all the packages and plugins that your project relies on, and their versions.
By the end of this tutorial, you will know how to:
- Add dependencies
- Update dependencies
- Remove dependencies
Prerequisites
- Basic knowledge of Flutter and Dart
- Flutter SDK installed on your machine
In Flutter, we manage dependencies using the pubspec.yaml
file located in the root of our project. This file contains metadata about the project and its dependencies.
To add a new dependency, we specify it under the dependencies
section in the pubspec.yaml
file.
For example, if we want to add the http
package, we add the following line under the dependencies
section:
dependencies:
http: ^0.13.3
The caret (^) in front of the version number means that flutter can use any version that is compatible with the specified version.
To update a package, you can change the version number in the pubspec.yaml
file and run flutter pub get
in the terminal to get the specified version.
To remove a package, simply delete the corresponding line in the pubspec.yaml
file and run flutter pub get
again.
Let's add the http
package to our Flutter project.
# pubspec.yaml
name: my_project
description: A new Flutter project.
#...
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
http: ^0.13.3 # Add this line
#...
Then run flutter pub get
in your terminal. Flutter will download and link the package to your project.
Let's update the http
package to a newer version.
# pubspec.yaml
#...
dependencies:
#...
http: ^0.13.5 # Update the version here
#...
Run flutter pub get
to fetch the new version.
To remove the http
package, delete its line from the pubspec.yaml
file.
# pubspec.yaml
#...
dependencies:
#...
# http: ^0.13.5 # Delete this line
#...
Run flutter pub get
again to update your project's dependencies.
In this tutorial, we've learned how to manage dependencies in a Flutter project. We've covered how to add, update, and remove dependencies using the pubspec.yaml
file.
As a next step, you can explore other dependency management features in Flutter, such as dependency overriding and using local packages.
Add the provider
package to your Flutter project. The latest version at the time of writing is 5.0.0
.
Update the provider
package to the latest version available.
Remove the provider
package from your project.
Solutions
provider: ^5.0.0
under the dependencies
section in pubspec.yaml
, then run flutter pub get
.provider
on pub.dev, update the version number in pubspec.yaml
, then run flutter pub get
.provider
in pubspec.yaml
, then run flutter pub get
.