46

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
Evandro Pomatti
  • 10,210
  • 13
  • 76
  • 141

4 Answers4

83

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

I hope it clears everything, happy coding!

Apoorv Pandey
  • 1,229
  • 1
  • 8
  • 11
23

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.

Community
  • 1
  • 1
Suragch
  • 428,106
  • 278
  • 1,284
  • 1,317
  • 1
    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? – Daniel Gomez Rico Jul 25 '20 at 21:49
  • 1
    @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.) – Suragch Jul 25 '20 at 23:46
  • 1
    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) – Daniel Gomez Rico Jul 26 '20 at 16:31
  • https://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! – Apoorv Pandey May 16 '21 at 09:19
  • @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? – Giacomo M Sep 25 '21 at 05:22
  • @GiacomoM, Yes, that's right. (Or on `flutter pub get` if you don't have a pubspec.lock file.) See [All about the Pub command line tool for Flutter and Dart](https://suragch.medium.com/all-about-the-pub-command-line-tool-for-flutter-and-dart-52339d594d96). – Suragch Sep 27 '21 at 07:08
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"

Richard Heap
  • 41,090
  • 7
  • 110
  • 94
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.

Evandro Pomatti
  • 10,210
  • 13
  • 76
  • 141