116

My Flutter project has a dependency flutter_dotenv at version ^2.0.1 and I want to automatically upgrade to the new version ^2.0.2.

I am running the following command to upgrade it:

flutter pub upgrade

Reference: Upgrading packages only

To update to the latest compatible versions of all the dependencies listed in the pubspec.yaml file, use the upgrade command:

flutter pub upgrade

However nothing seems to happen. pubspec.yaml does not change, and the console output does not mention of a new version (which would be enough).

My pubspec.yaml looks like this:

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_dotenv: ^2.0.1
  cupertino_icons: ^0.1.2

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true
0

9 Answers 9

213

Above method works but you can use this command:

flutter pub upgrade --major-versions

It will update all your dependencies.

Also check "How to correctly add dependencies to avoid "Version solving failed" error

Refer to this: https://stackoverflow.com/a/67517680/13500457

Sign up to request clarification or add additional context in comments.

3 Comments

will this update pubspec.yaml as well?
Yes absolutely @giorgio79
As of 2 weeks ago there is now a --tighten flag, that will update pubspec.yaml for minor changes as well.
40

Flutter automatically upgrades non-breaking changes based on semantic versioning. You wouldn't want breaking changes to be automatic. The updates are reflected in pubspec.lock, but not pubspec.yaml.

There are a couple IDE plugins that can help you to upgrade packages more easily than looking them up one by one on pub.dev.

Android Studio

Flutter Pub Version Checker

This plugin highlights any dependencies in pubspec.yaml you have that are out of date so that you can choose to update them if you want to.

Visual Studio Code

Pubspec Assist

This plugin makes it super simple to add or update a dependency without going to pub.dev, but you still have to check them one at a time.

6 Comments

I dont agree with "you wouldnt want breaking changes to be automatic", I prefer to think more like "you dont want any change to be automatic", the posibility of introducing problems based on this is incredible, WDYT?
@DanielGomezRico, If you don't want any change to be automatic then you can remove the ^ from in front of the version number. However, as long as package developers are following semantic versioning, leaving the ^ is fine, since by definition there will be no breaking changes. But even if there are breaking changes, you still test your app before you release it. In my experience I've only had one experience where a minor change broke something. (And that was Dart 2.8 itself.)
To me the pain was from some old projects that were made like 2 or 3 years ago, and made them recompile was horrible, finding which of which dependency (there was a list of like 30 dependencies)
stackoverflow.com/a/66759292/13500457 When you use the method you've mentioned it'll take some time, when you move your cursor and press alt + enter to all dependencies so it's better to use this command " flutter pub upgrade --major-versions " also you will not get " Version solving error due to SDK version incompatibility" so use this refer the link for details. Happy coding!
@Suragch sorry to comment an old answer, but I have a doubt: when you say "Flutter automatically upgrades non-breaking changes based on semantic versioning.", you mean, when you use flutter pub upgrade command?
|
16

flutter pub upgrade --major-versions --tighten

Comments

8

Running pub won't ever change pubspec.yaml. However, it might solve to a version different from the 'base' version specified - the leading caret allows pub to solve to:

the range of all versions guaranteed to be backwards compatible with the specified version

Check in the pubspec.lock file and you'll probaby see that pub has already solved to version: "2.0.2"

2 Comments

so the answer to OP "How to automatically upgrade Flutter dependencies" is... There is no way?
@adrianvintu yes there is, check my own answer for this.
7

For upgrading from very old versions to null safety versions:

//Upgrading flutter sdk
flutter upgrade
//Upgrading dart code
dart migrate
// Upgrading all pubspec.yaml package versions
flutter pub outdated --mode=null-safety

// Download all new versions of the packages
dart pub get

Upgrading dart code again. At this stage you should correct all issues (manually or using migration guide otherwise "The migration tool didn't start, due to analysis errors" show up.

// See list of available fixes
dart fix --dry-run

// Fix all issues automatically
dart fix --apply

Refer to this article for more details

Check Flutter migration guide here

Comments

3

There are two ways of declaring dependency versions:

  1. Caret syntax - It guarantees backwards compatibility. Example: ^1.3.0
  2. Traditional syntax - Great flexibility, many options for your control. Example: >=1.2.3

The behavior is similar to package.json with Node.js dependency management.

Your chosen way of declaring dependencies in the pubspec.yaml will define how the actual dependencies will be defined in the pubspec.lock file.

1 Comment

Error detected in pubspec.yaml: Error on line 11, column 20: Expected comment or line break. ╷ 11 │ add_2_calendar: >=2.1.3
3

Even if running flutter pub upgrade --major-versions (as answered here) doesn't change the pubspec.yaml file, try:

flutter pub add [dependency]

The command above will show the message "[dependency]" is already in "dependencies". Will try to update the constraint., and reflect the newest dependency version on the pubspec.yaml, if there's one.

3 Comments

I use flutter pub upgrade --major-versions and it works well
@AbdullahBahattab The alghorithm of pub upgrade --major-versions doesn't update all possible constraints in the pubspec. I don't know its algorithm, but it seems it updates only those constraints that are pushed to a new major version and it will also only update so constraints don't break. pub add will ensure the package will get an updated constraint in the pubspec.yaml which leads to possible constraint violations, since it pushes to the latest possible version. It's clearly a useful option when modernizing an app by lifting all dependencies and then manually fixing constraints.
A way to automate this is doing yq '.dependencies | to_entries | .[] | select(.value|type == "!!str") | .key' pubspec.yaml | xargs -L1 flutter pub add or yq '.dev_dependencies | to_entries | .[] | select(.value|type == "!!str") | .key' pubspec.yaml | xargs -L1 flutter pub add -d respectively.
0

I feel like the other answers are dancing around your issue.

You want your upgraded changes to reflect inside pubspec.yaml then use --tighten.

From dart pub upgrade --help:

--tighten Updates lower bounds in pubspec.yaml to match the resolved version.

Comments

0

To automatically upgrade flutter deps, you can try using the command below

flutter pub upgrade --major-versions

This command will attempt to update all dependencies listed in your pubspec.yaml file to their latest available versions, even if it means upgrading to a new major version (which might introduce breaking changes).

P.S.

  • Upgrading major versions can introduce breaking changes. After running this command, thoroughly test your application and resolve any errors that arise.

    Alternatively, you can check for the outdated packages**,** to see which packages are outdated without performing an upgrade, use the command below:

    flutter pub outdated

For more details, refer to the Flutter official docs: Flutter upgrade official docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.