84

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
  • 13,341
  • 16
  • 97
  • 165

6 Answers6

160

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

starball
  • 20,030
  • 7
  • 43
  • 238
Apoorv Pandey
  • 2,118
  • 2
  • 11
  • 13
34

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
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 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
7

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
  • 48,344
  • 9
  • 130
  • 112
3

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

Elmar
  • 2,235
  • 24
  • 22
2

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
  • 13,341
  • 16
  • 97
  • 165
  • Error detected in pubspec.yaml: Error on line 11, column 20: Expected comment or line break. ╷ 11 │ add_2_calendar: >=2.1.3 – Justin Nov 30 '22 at 18:39
0

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.

Filipe Brito
  • 5,329
  • 5
  • 32
  • 42